1. Yleid with generator (), Save status
2. User-state switching, switching is determined by the user
3. The process itself is a micro-threading
4. Co-integration is also for IO operations
5. No switching consumption
6. The concept of no lock
‘‘‘
(1) Generator function
def f (): print ("OK") syield5 Print (s) print ("ok2") yieldGen =f () x=gen.__next__ () print (x) gen.send (0)
(2) The producer consumer model is realized by yeild
Import Queueimport timeq=queue. Queue () def cousumer (name): Print ("ready to start eating steamed buns >>>>>>>>") whileTrue:new_baozi=yieldPrint ("Cousumer%s has eat%s Baozi"%(Name,new_baozi)) def Producer (name): R=con1.__next__ () R=con2.__next__ () n=0 while 1: Time.sleep (1) Print ("Producer%s is making Baozi%s and%s"% (name,n,n+1)) con1.send (n) con2.send (n+1) n+=2if__name__ = ='__main__': Con1=cousumer ("C1") Con2= Cousumer ("C2") P= Producer ("Chef No. 1th")
(3)-------------The switch between the Greenlet function is more convenient--------------------------
#通过Switch() switch at any time fromGreenlet Import Greenletdef too2 (): Print (898) Gr1.Switch() print ( the) def too1 (): Print ( the) gr2.Switch() print ("7897978") gr2.Switch() Gr1=Greenlet (too1) GR2=Greenlet (TOO2) # GR1.Switch() # Initiator of the Action Too1 ()
(4)-----------------Gevent interface encounters an IO wait operation for automatic switching---------------------
Import Requests,time,geventt1=Time.time () def f (URL): Print ("GET:%s"%URL) Resp=requests.Get(URL) data=Resp.text F= Open ("new.html","W", encoding="UTF8") f.write (data) print ("%d bytes recevied from%s"%(len (data), URL)) Gevent.joinall ([Gevent.spawn (F,"https://www.bilibili.com/video/av21663728/?p=325"), Gevent.spawn (F,"http://news.baidu.com/"), Gevent.spawn (F,"http://www.okoook.com/"), Gevent.spawn (F,"https://www.imooc.com/"), Gevent.spawn (F,"http://www.xiaohuar.com/"), Gevent.spawn (F,"http://www.xiaohuar.com/hua/"),] # print (Time.time ()-t1) #比较串行 # f ("https://www.bilibili.com/video/av21663728/?p=325") # f ("http://news.baidu.com/") #f ("http://www.okoook.com/") # f ("https://www.imooc.com/") # f ("http://www.xiaohuar.com/") # f ("http://www.xiaohuar.com/hua/") Print (Time.time ()-T1)
Python co-process