d2jsp
d2jsp Forums > Off-Topic > Programmer's Haven > Python - Multiprocessing
Add Reply New Topic New Poll
martinHC123
#1 Mar 6 2021 11:51am
Group: Member
Posts: 1,669
Joined: Jul 13 2009
Gold: 895.26
Hi,

can anyone help me please with code bellow? The task is to run function device_a as new process, that will every second print setting value. I would like to change values for the function from the main code.
The problem with the code is, that it gets stucked until I enter new value in the input.

I appreciate any help, so here isthe code:

from multiprocessing import Process, Pipe
from time import sleep


def device_a(connection):

counter = 0

while True:
sleep(1)
t = connection.recv()
counter += 1
print('device_a at {} received: {}'.format(counter, t))

if __name__ == '__main__':

parent_conn, child_conn = Pipe()
p = Process(target=device_a, args=(child_conn,))
p.start()

while True:

setting = input('Enter a setting: ')
parent_conn.send(setting)
cmetc1999
#2 Mar 6 2021 07:00pm
Group: Member
Posts: 7,244
Joined: Jun 9 2007
Gold: 4.80
i am not entirely certain what you're trying to do, but using Manager to share the variable will probably get you closer to where you want to be.

Code
from ctypes import c_char_p
from multiprocessing import Process, Manager
from time import sleep

def device_a(setting):
counter = 0

while True:
sleep(1)
counter += 1
print(f'device_a at {counter} received: {setting.value}')

if __name__ == '__main__':a
# use manager to share setting across processes
manager = Manager()
shared_setting = manager.Value(c_char_p, "") # defaults to empty string, can prompt earlier or do whatever

# process
Process(target=device_a, args=(shared_setting,)).start()

# main thread
while True:
setting = input('Enter a setting: ')
shared_setting.value = setting


This post was edited by cmetc1999 on Mar 6 2021 07:00pm
martinHC123
#3 Mar 7 2021 01:46am
Group: Member
Posts: 1,669
Joined: Jul 13 2009
Gold: 895.26
Quote (cmetc1999 @ Mar 7 2021 03:00am)
i am not entirely certain what you're trying to do, but using Manager to share the variable will probably get you closer to where you want to be.

Code
from ctypes import c_char_p
from multiprocessing import Process, Manager
from time import sleep

def device_a(setting):
counter = 0

while True:
sleep(1)
counter += 1
print(f'device_a at {counter} received: {setting.value}')

if __name__ == '__main__':a
# use manager to share setting across processes
manager = Manager()
shared_setting = manager.Value(c_char_p, "") # defaults to empty string, can prompt earlier or do whatever

# process
Process(target=device_a, args=(shared_setting,)).start()

# main thread
while True:
setting = input('Enter a setting: ')
shared_setting.value = setting


Thanks, so far your posted code does what I need. I am trying to run independently some external devices (motors, sensors, ect.) on my rapsberry pi, and from time to time want be albe to change the settings. Tried google before, and it suggested multiprocessing, which is pretty new for me (I do programming, and projects for fun).

Maybe there are some other ways to do it more effectively... have to do some more research.
Klexmoo
#4 Mar 7 2021 03:46pm
Group: Member
Posts: 12,641
Joined: May 17 2013
Gold: 1,000.00
use the ProcessPoolExecutor from concurrent.futures, it's very simple to use
https://docs.python.org/3/library/concurrent.futures.html

Code

from concurrent.futures import ProcessPoolExecutor

with ProcessPoolExecutor(max_workers=4) as executor:
result_futures = []
for data in iterator:
result_futures.append(executor.submit(processing_func, data))

results = [f.result() for f in result_futures]

Go Back To Programmer's Haven Topic List
Add Reply New Topic New Poll