Multi-process + Semaphore semaphore, process signal Semaphore

Source: Internet
Author: User

Multi-process + Semaphore semaphore, process signal Semaphore

The following is an example.

from multiprocessing import Processfrom multiprocessing import Semaphoreimport datetimeimport timeimport multiprocessingdef worker(s, i):    s.acquire()    print(multiprocessing.current_process().name + " acquire",datetime.datetime.now())    time.sleep(i)    print(multiprocessing.current_process().name + " release",datetime.datetime.now(),"\n")    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()

Running result:

Analysis: p = multiprocessing. Process (......) Five processes and five p. start processes are defined in parallel. The result is that the semaphore restricts the process's access to critical resources. S = multiprocessing. semaphore (2) defines the maximum Semaphore value 2, release: + 1 acquire:-step 1, five processes run concurrently, process 1 executes and waits for 0 s, s-1 = 1 second step, five processes run concurrently, process 2 execution and wait 2 s, S-1 = 0 third step, because process 1 is executed, and the wait time is 0, process 2 needs to wait 2 s. Therefore, this step must be executed by process 1, and the execution of process 1 is completed, while the semaphore + 1 is not blocked. Step 4, process 2 enters wait 2 s, so only three processes 3, 4, 5 parallel, process 4 Execution and wait 6 s, S-1 = fifth step, process 2 waits for 2 seconds to complete, process 2 executes, S + 1 = 1 step 6, process 4 waits, only two processes 3 and 5 are in parallel, process 3 executes and waits for 4 seconds, s-1 = Step 7, process 3 and 4 wait for time to end at the same time, process 3 compete for and critical resource execution, S + 1 = Step 8, process 4 wait, process 5 Execution and wait 8 s, S-1 = 0 ninth step, process 4 Execution, S + 1 = step 10, process 5 Execution, S + 1 = 1 Reference: http://www.cnblogs.com/kaituorensheng/p/4445418.html

Related Article

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.