Python multiprocessing manager

My problem:

I have 3 procs that would like to share a configuration loaded from the same class and a pair of queues. I would like to create another proc as multiprocessing.manager to share this information.

How can i do this? Can anyone get sample code by avoiding using global vars and using the multiprocessing manager class?

Python docs were not very useful: - (

+3
source share
1 answer

I found this specific section in the Python multiprocessing documentation. The following program:

from multiprocessing import Process, Queue, current_process
import time

def f(q):
    name = current_process().name
    config = q.get()
    print "%s got config: %s" % (name, config)
    print "%s beginning processing at %s" % (name, time.asctime())
    time.sleep(5)
    print "%s completing processing at %s" % (name, time.asctime())

if __name__ == '__main__':
    q = Queue()
    processes = []
    cfg = { 'my' : 'config', 'data' : 'here' }
    for i in range(3):
        p = Process(target=f, args=(q,))
        processes.append(p)
        p.start()
        q.put(cfg)

    for p in processes:
        p.join()

script ( " " ), 3 ( ).

, (, 5 ), . script :

Process-1 got config: {'my': 'config', 'data': 'here'}
Process-1 beginning processing at Tue Jun 23 23:34:23 2009
Process-2 got config: {'my': 'config', 'data': 'here'}
Process-2 beginning processing at Tue Jun 23 23:34:23 2009
Process-3 got config: {'my': 'config', 'data': 'here'}
Process-3 beginning processing at Tue Jun 23 23:34:23 2009
Process-1 completing processing at Tue Jun 23 23:34:28 2009
Process-2 completing processing at Tue Jun 23 23:34:28 2009
Process-3 completing processing at Tue Jun 23 23:34:28 2009
+3

Source: https://habr.com/ru/post/1711122/


All Articles