Coroutine Study Notes and Study Notes

Source: Internet
Author: User

Coroutine Study Notes and Study Notes

I,Coroutine Introduction

What is coroutine?

Coroutine, also known as micro-thread, thread, English name Coroutine. Coroutine is a lightweight thread in user mode.

Coroutine has its own register context and stack.

To put it simply, coroutine switches back and forth. When I/O operations, such as reading and writing files and network operations, I/O operations are executed in another thread, I/O operations, and I/O operations. Continuously jump over and execute, because the speed is very fast, so it looks like the execution of concurrency, essentially a single thread.

Benefits of coroutine:

Disadvantages of coroutine:

1. multi-core resources cannot be used

Coroutine is essentially a single thread and cannot be used by multiple cores of a single CPU at the same time.
The coroutine must be used together with the process to run on multiple CPUs. Most applications written in daily work do not need this unless it is a CPU-intensive application.

2. Blocking operations (such as reading and writing files and networks during IO) will block the entire program.

II,Yield implements the simplest coroutine Effect

Concurrent effects in a single thread

Taking a simple producer-consumer model as an example, the producer Cook makes steamed stuffed bun and the consumer eats steamed stuffed bun

Code 1:

Note: One producer, one steamed stuffed bun at a time, only five and two consumers

1 import time 2 import queue 3 4 def consumer (name): 5 print ("% s ready to start eating steamed stuffed bun... "% name) 6 while True: 7 new_baozi = yield 8 print (" [% s] Eating Steamed Stuffed Bun % s "% (name, new_baozi) 9 # time. sleep (1) 10 11 def producer (): 12 r = con. _ next _ () 13 r = con2. _ next _ () 14 n = 015 while n <5: # The producer only needs 5 steamed stuffed buns 16 n + = 117 print ("\ 033 [32; 1 m [producer] \ 033 [0 m has made the new Steamed Stuffed Bun % s" % n) 18 time. sleep (1) 19 con. send (n) 20 con2.send (n) 21 22 if _ name _ = '_ main __': 23 # define 2 consumers and 1 producer 24 con = consumer ("c1") 25 con2 = consumer ("c2") 26 p = producer () 27 28 # display results: 29 # c1 prepare to start eating steamed stuffed bun... 30 # c2 prepare to start eating steamed stuffed bun... 31 # [producer] New Steamed Stuffed Bun 132 # [c1] Eating Steamed Stuffed Bun 133 # [c2] Eating Steamed Stuffed Bun 134 # [producer] New Steamed Stuffed Bun 235 # [c1] is eat steamed stuffed bun 236 # [c2] Eating Steamed Stuffed Bun 237 # [producer] New Steamed Stuffed Bun 338 # [c1] Eating Steamed Stuffed Bun 339 # [c2] Eating Steamed Stuffed Bun 340 # [producer] made a new Steamed Stuffed Bun 441 # [c1] Eating Steamed Stuffed Bun 442 # [c2] Eating Steamed Stuffed Bun 443 # [producer] made a new Steamed Stuffed Bun 544 # [c1] Eating Steamed Stuffed Bun 545 #[ c2] Eating Steamed Stuffed Bun 5

Code 2:

Note: The producer only makes five steamed buns, two at a time and two consumers at a time.

1 import time 2 import queue 3 4 def consumer (name): 5 print ("% s ready to start eating steamed stuffed bun... "% name) 6 while True: 7 new_baozi = yield 8 print (" [% s] Eating Steamed Stuffed Bun % s "% (name, new_baozi) 9 # time. sleep (1) 10 11 def producer (name): 12 r = con. _ next _ () 13 r = con2. _ next _ () 14 n = 015 m = 016 count = 017 while count <5: # The producer only makes five steamed buns, 2 18 n = m + 119 m = n + 120 count + = 121 print ("\ 033 [32; 1 m cook % s \ 033 [0 m new Steamed Stuffed Bun % s "% (name, n) 22 print (" \ 033 [32; 1 m cook % s \ 033 [0 m new Steamed Stuffed Bun % s "% (name, m) 23 time. sleep (1) 24 con. send (n) 25 con2.send (m) 26 27 if _ name _ = '_ main __': 28 # define 2 consumers and 1 producer 29 con = consumer ("Lili") 30 con2 = consumer ("quiet") 31 p = producer (" ") 32 33 # result: 34 # Lily is going to start eating steamed stuffed bun... 35 # quietly prepare to start eating steamed stuffed bun... 36 # New Steamed Stuffed Bun made by Chef Xiao Bai 137 # New Steamed Stuffed Bun made by Chef Xiao 238 # [Lily] Eating Steamed Stuffed Bun 139 # [Quiet] Eating Steamed Stuffed Bun 240 # New Steamed Stuffed Bun made by Chef Xiao 341 # New Steamed Stuffed Bun 442 # [Lili] is eating Steamed Stuffed Bun 343 # [Quiet] is eating Steamed Stuffed Bun 444 # New Steamed Stuffed Bun 545 # New Steamed Stuffed Bun made by Chef Xiaobai 646 # [Lili] Eating Steamed Stuffed Bun 547 # [Quiet] Eating Steamed Stuffed Bun 648 # New Steamed Stuffed Bun 749 # New Steamed Stuffed Bun 850 # [Lili] Eating steamed stuffed bun 751 # [Quiet] Eating Steamed Stuffed Bun 852 # New Steamed Stuffed Bun 953 prepared by Chef Xiao # New Steamed Stuffed Bun 1054 prepared by Chef Xiao # [Lily] Eating Steamed Stuffed Bun 955 # [Quiet] Eating steamed stuffed bun 10

 

Simulate IO operation blocking with sleep
Yield cannot implement how to throw blocking to the operating system when a blocking occurs. It can be implemented through coroutine.

Iii. greenlet Module

Greenlet, A coroutine module, is a third-party module that is not provided by Python and can be used only after installation.
Import module command: from greenlet import greenlet

Sample Code:

1 from greenlet import greenlet 2 3 def test1 (): 4 print (12) 5 gr2.switch () 6 print (34) 7 gr2.switch () 8 9 def test2 (): 10 print (56) 11 gr1.switch () 12 print (78) 13 14 gr1 = greenlet (test1) 15 gr2 = greenlet (test2) 16 gr1.switch () 17 18 # display results: 19 #1220 #5621 #3422 #7823 24 # Note: first run the test1 function, print 12, switch to the test2 function, print 56, and then return to test1 (previously switched ), print 34, switch to test again, print 7825 # coroutine, that is, switch back and forthGreenlet module coroutine example

 

Write it here first...

 

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.