d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python - Multiprocessing
Add Reply New Topic New Poll
Member
Posts: 1,676
Joined: Jul 13 2009
Gold: 1,687.26
Mar 6 2021 11:51am
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)
Member
Posts: 7,254
Joined: Jun 9 2007
Gold: 4.49
Mar 6 2021 07:00pm
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
Member
Posts: 1,676
Joined: Jul 13 2009
Gold: 1,687.26
Mar 7 2021 01:46am
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.
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Mar 7 2021 03:46pm
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 Programming & Development Topic List
Add Reply New Topic New Poll