13.1, multi-process: Process lock lock, semaphore, event

Source: Internet
Author: User
Tags semaphore

Process Lock:
    • Why should have process lock: If now have a printer, QQ to use printer, Word document also want to use printer, if not using process lock, may cause some problems, such as QQ task Print to half, word inserted, so print out the result is the respective half.
    • Process lock creation and use:
      • 1. Import module, from multiprocessing import Lock
      • 2. Create lock object: Lock=lock ()
      • 3. Where the lock is needed: Lock.acquire ()
      • 4. In place of release: Lock.release ()
 fromMultiprocessingImportlock,processdeff (l,num): L.acquire ()Try:        Print("printer used done", num)finally: L.release ()if __name__=="__main__": Lock=Lock () forNuminchRange (10): Process (Target=f, args= (Lock,num)). Start ()

Later to meet better examples to do a supplement.

Signal Volume:
    • The semaphore can limit the number of incoming processes.
    • How to use the semaphore:
      1. Create Semaphore object: Semaphore Object =multiprocessing. Boundedsemaphore (x), X is the number of limit processes
      2. When a process needs to enter, call acquire () to reduce the semaphore: Semaphore object. Acquire ()
      3. When a process leaves, call release () to increase the semaphore: Semaphore object. Release ()

Later to meet better examples to do a supplement.

Event:
    • How to use events:
      1. Create Event object: Event Object =multiprocessing. Event ()
      2. Set Event: Event object. Set () determines whether the event is set: Event object. Is_set (), Waiting for event set: Event object. Wait ()
      3. Purge Event: Event object. Clear ()
later to meet better examples to do a supplement.

13.1, multi-process: Process lock lock, semaphore, event

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.