Python Quest path 9--io& Queue & Cache

Source: Internet
Author: User

Co-process:

1, single-threaded operation, can not achieve multi-threading.

2, modify the data do not need to lock (single-threaded run), the sub-program switch is the thread internal switch, time-consuming.

3, a CPU can support tens of thousands of processes, suitable for high concurrency processing.

4. Cannot take advantage of multi-core resources because the process has only one thread.

Use yield to implement the process:

Import TimeImportQueuedefConsumer (name):Print("--->starting eating baozi ...")     whileTrue:new_baozi=yield        Print("[%s] is eating Baozi%s"%(Name,new_baozi))#time.sleep (1)defproducer (): R= Con.next ()#the function that has yield is the iteration, using the next () method to take the value. R =Con2.next () n=0 whileN < 5: N+=1con.send (n) con2.send (n)Print("\033[32;1m[producer]\033[0m is making Baozi%s"%N)if __name__=='__main__': Con= Consumer ("C1") Con2= Consumer ("C2") P= producer ()

Python Quest path 9--io& Queue & Cache

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.