Python thread Threading (v)

Source: Internet
Author: User

After learning the event and condition two thread synchronization tools, there is a tool I think is more chicken semaphores

1. The effect of using semaphores and the condition notify method are basically the same. Only one blocking thread can be notified to continue running at a time

2. Semaphore synchronization is based on internal counters, each call acquire (), the counter minus 1, each call to release (), the Counter plus 1. When the counter is 0 o'clock, the acquire () call is blocked

1 ImportThreading2 Import Time3 4 defCountdown (N, SEMA):5      whilen >0:6N-= 17 Sema.acquire ()8         Print('Current countdown:', N)9             Ten  One defcountup (n, SEMA): A      whileN < 100: -n + = 1 - Sema.acquire () the         Print('Current Countup:', N) -  -SEMA =Threading. Semaphore () -Threading. Thread (target=countdown,args= (100, SEMA)). Start () +Threading. Thread (target=countup,args=(0, SEMA)). Start () -  forIinchRange (100): + sema.release () ATime.sleep (3)

Python thread Threading (v)

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.