python multi-process shared global variables of the Manager ()

Manager Supported types list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Queue, Value and Array.

However, when using Manager process list, dict variable data types and the like, a trap to be noted that the Manager can not monitor the object to modify the value of a variable that references the object, triggered by the need __setitem__to make it notified method.

Triggering __setitem__method is relatively straightforward way is to increase an intermediate variable, as two variables as the exchange in the C language: int a = 1; int b = 2; int tmp = a; a = b; b = tmp;

python examples:

 1 from multiprocessing import Manager,Process
 2 
 3 def test_manager():
 4 
 5   m[0]['id'] = 2
 6 
 7 m = Manager().list()
 8 
 9 m.append({"id":1})
10 
11 p = Process(target=test_manager)
12 
13 p.start()
14 
15 p.join()
16 
17 print m[0]

Results of the:

{"id":1}

Has not changed

Modify test_manager ()

def test_manager():
    tmp = m[0]
    tmp{"id"} = 2
    m[0] = tmp

Run the result is:

{"id":2}

Further, for the objects to be noted Process can be serialized to pickle

 

Guess you like

Origin www.cnblogs.com/fdzwdt/p/11357195.html
Recommended