A coroutine package commonly used in python
yield , greenlet , peddled , asyncio
Second, create a coroutine
1) using the yield to create a coroutine
import time DEF Consumer (name): # Builder Print ( " !% S to start a bun " % name) the while True: Baozi = the yield # pause, recording position, out return (send receive data transmitted from below, after receiving the data will continue) Print ( " bun% s,% s eat " % (Baozi, name)) def producer(name): C = Consumer ( " consumer " ) # only into a generator C. __next__ () # Next wakes up only not transmitted yield value for I in Range (. 4 ): the time.sleep ( . 1 ) Print ( " % S S made buns% " % (name, I)) c.send (I) # transmit data if __name__ == "__main__": Producer ( " Producer " ) result: Consumers want to start a bun! Producers made buns 0 0 buns, consumers eat Producers made buns 1 Steamed bun, consumers eat Producers made buns 2 2 buns, consumers eat Producers made buns 3 3 buns, consumers eat
2) Use greenlet create coroutine
from greenlet import greenlet def proc1(): print(12) gr2.switch () # perform switching to the right gr2, gr1 suspended Print (34 is ) gr2.switch () # implementation of the right to switch to local gr2, gr1 will just pause to continue def proc2(): print(56) gr1.switch () # implementation of the right to switch to local gr1, gr1 will just continue to suspend execution, gr2 suspend Print (78 ) if __name__ == "__main__": gr1 = greenlet (Proc1) # start coroutine. 1 GR2 = greenlet (Proc2) # start coroutine 2 gr1.switch () # to perform the specified functions run gr1 result: 12 56 34 78
3) Use gevent create coroutine
from gevent Import Monkey; monkey.patch_all () # This represents the IO encountered on the transfer of executive powers, without waiting for Import gevent Import Requests def f(url): print('GET: %s' % url) RESP = requests.get (URL) # network switches execution right IO Data = resp.text Print ( ' % S% D bytes Received from. ' % (len (Data), URL)) gevent.joinall ([ gevent.spawn (f, ' https://www.baidu.com/ ' ), gevent.spawn (f, ' https://www.sina.com.cn/ ' ), gevent.spawn (f, ' https://sohu.com/ ' ), ]) result: GET: https://www.baidu.com/ GET: https://www.sina.com.cn/ GET: https://sohu.com/ Received bytes 537 854 from https://www.sina.com.cn/ . 2443 bytes Received from https://www.baidu.com/ . 177 385 bytes Received from https://sohu.com/ .
Conclusion: When faced with IO when the executive power is switched on, and to perform other tasks, such as network and then come back and continue with the IO write down import gevent import requests def f(url): print('GET: %s' % url) RESP = requests.get (URL) # network switches execution right IO Data = resp.text Print ( ' % S% D bytes Received from. ' % (len (Data), URL)) gevent.joinall ([ gevent.spawn (f, ' https://www.baidu.com/ ' ), gevent.spawn (f, ' https://www.sina.com.cn/ ' ), gevent.spawn (f, ' https://sohu.com/ ' ), ]) result: GET: https://www.baidu.com/ 2443 bytes received from https://www.baidu.com/. GET: https://www.sina.com.cn/ 537846 bytes received from https://www.sina.com.cn/. GET: https://sohu.com/ 177385 bytes received from https://sohu.com/. Conclusion: If you do not monkey.patch_all (), then perform one task coroutine will, when faced with the IO, it will wait for the content to return, go to the next task
3) Use asyncio create coroutine
import time import asyncio ''' Examples of single coroutine ''' now = time.time () # returns the current timestamp the async DEF do_some_work (X): # define coroutine task prefix must write the async Print ( ' Waiting: ' , X) time.sleep(x) if __name__ == "__main__": start = time.time() coroutine1 = do_some_work (2) # the coroutine task objects stored in the variables coroutine2 do_some_work = (. 3 ) Loop = asyncio.get_event_loop () # Create an event loop loop.run_until_complete (coroutine1) # The coroutine registered to the event loop, and start the event loop loop.run_until_complete (coroutine2) # The coroutine registered to the event loop, and start the event loop Print ( ' the TIME: ' , the time.time () - Start) result: Waiting: 2 Waiting: 3 TIME: 5.001837491989136
import time import asyncio ''' Bind callbacks ''' async def do_some_work(x): # 协程任务,前缀async print('Waiting: ', x) return 'Done after {}s'.format(x) DEF the callback (A): # A representative of task objects coroutine Print ( ' the Callback: ' , a.result ()) # a.result () fixed wording, acquisition task coroutine return value. if __name__ == "__main__": start = time.time() the coroutine = do_some_work (2) # The task coroutine objects stored in the variables Loop = asyncio.get_event_loop () # the coroutine registered to the event loop, and start the event loop Task = asyncio.ensure_future (the coroutine) # create a Task Task .add_done_callback (callback) # bind callback loop.run_until_complete (Task) # the test registered to the event loop, and start the event loop Print ( ' the TIME: ' , time.time () - start)
results:
Waiting: 2
Callback: Done after 2s
TIME: 0.0009970664978027344