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