Python class library 32 [multi-process synchronization lock + semaphore + event]

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.