Python multi-process synchronization Lock, Semaphore, Event instance

Source: Internet
Author: User
This article mainly introduces Python multi-process synchronization Lock, Semaphore, and Event instances. Lock is used to avoid access conflicts, Semaphore is used to control the number of accesses to shared resources, and Event is used to implement synchronous communication between processes, for more information, see the synchronization method.

1) Lock

When multiple processes need to access shared resources, Lock can be used to avoid access conflicts.

The code is as follows:


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.

The code is as follows:


Import multiprocessing
Import time

Def worker (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)
For I in range (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.

The code is as follows:


Import multiprocessing
Import time

Def wait_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 _ = '_ 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

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.