The synchronization method is basically the same as that of multithreading.
1) Lock
When multiple processes need to access shared resources, lock can be used to avoid access conflicts.
Import Multiprocessing
Import sys
Def worker_with ( Lock , F ):
With Lock :
FS = open (F, " A + " )
FS. Write ( ' Lock acquired via with \ n ' )
FS. Close ()
Def worker_no_with ( Lock , F ):
Lock . Acquire ()
Try :
FS = open (F, " A + " )
FS. Write ( ' Lock acquired directly \ n ' )
FS. Close ()
Finally :
Lock . Release ()
If_ Name _ ="_ Main __":
F ="File.txt"
Lock= Multiprocessing. Lock ()
W = multiprocessing. Process (target = worker_with, argS = (Lock, F ))
Nw = multiprocessing. Process (target = worker_no_with, argS = (Lock, F ))
W. Start ()
NW. Start ()
W. Join ()
NW. Join ()
In the above example, if the two processes do not use lock for synchronization, their write operations on the same file may be messy.
2) semaphore
Semaphore is used to control the number of accesses to shared resources, such as the maximum number of connections to the pool.
Import Multiprocessing
Import Time
DefWorker (S, I ):
S. Acquire ()
Print(Multiprocessing. current_process (). Name +"Acquire")
Time. Sleep (I)
Print(Multiprocessing. current_process (). Name +"Release")
S. Release ()
If _ Name __="_ Main __":
S = multiprocessing. semaphore (2)
ForIInRange (5 ):
P = multiprocessing. Process (target = worker, argS = (S, I * 2 ))
P. Start ()
In the above example, semaphore is used to limit the simultaneous execution of a maximum of two processes.
3) event
Event is used to implement synchronous communication between processes.
Import Multiprocessing
Import Time
DefWait_for_event (e ):
"""Wait for the event to be set before doing anything"""
Print('Wait_for_event: Starting')
E. Wait ()
Print('Wait_for_event: E. is_set ()->'+ STR (E. is_set ()))
def wait_for_event_timeout (E, T):
" wait t seconds and then timeout " "
Print ( ' wait_for_event_timeout: Starting ')
E. wait (t)
Print ( ' wait_for_event_timeout: E. is_set ()-> ' + STR (E. is_set ()
If __ name __=< span style =" color: #800000; "> ' __ main __ ':
E = multiprocessing. event ()
W1 = multiprocessing. process (name = ' block ',
Target = wait_for_event,
ARGs = (E,)
w1.start ()
W2 = multiprocessing. Process (name ='Non-block',
Target = wait_for_event_timeout,
ARGs = (E, 2 ))
W2.start ()
Time. Sleep (3)
E. Set ()
Print (' Main: event is set ' )
# The output is:
# Wait_for_event_timeout: Starting
# Wait_for_event: Starting
# Wait_for_event_timeout: E. is_set ()-> false
# Main: event is set
# Wait_for_event: E. is_set ()-> true
Reference: http://www.doughellmann.com/PyMOTW/multiprocessing/communication.html
Complete!