One, the Association Process: 1, the generator only when the call will generate the corresponding data 2, the call mode has "str__next__." () Str.send () ", 3, and each call produces a value call to the last value will be an error 4, error can be done by try and except to do exception handling note: Next: is directly called yield, and will not pass the value. Send: is called and the value is passed directly to yield.
1 #!/usr/bin/env python2 #-*-Coding:utf8-*-3 #Author:dong Ye4 5 6 " "7 define two models:8 one is the production of steamed buns. (generator)9 the other is to eat steamed buns. (iterator)Ten One This function realizes the embryonic form of asynchronous IO and is also a simple process of processing. A the characteristics of the co-process: The actual serial mode is performed separately, but because of the fast running effect, give people feel like parallel. - Therefore, the co-process is also known as: single-threaded parallel execution effect. - A thread is a unit of a unit that contains an online, thread-in-progress process. the For example: Enginx in asynchronous single-threaded, much faster than multithreading, which is the effect. - " " - - Import Time + - + #to eat steamed buns A defConsumer (name): atPrint('%s ready to eat buns! '%name) - whileTrue: -Baozi =yield -Print("the bun [%s] came. Eaten by [%s]! "%(baozi,name)) - - in #production of steamed buns - defproducer (name): to #Define 2 threads (consumers) first #将函数变成生成器 +C1 = Consumer ('A')#2 Consumers -C2 = Consumer ('B')#equivalent to 2 threads (process, thread, coprocessor) the #Start Call generator initialization (prepare to eat buns) *C1.__next__()#start calling the generator and only next will go to yield for the next action $C2.__next__()Panax NotoginsengPrint('Lao Zi starts to eat steamed bun to pull! ') - #the number of cycles, each cycle will pass value to the generator (what kind of bun produced) the forIinchRange (10): +Time.sleep (1) APrint("made a bun, divided in 2 and a half, one half") theC1.send (i)#Types of Buns + c2.send (i) - $ $Producer"Alex") - - the - " "Wuyi #手动做包子: the C = Consumer ("dy") - c.__next__ () Wu #c. __next__ () - About B1 = "Leek Stuffing" $ C.send (B1) #调用 + Pass value - #c. __next__ () #只调用, no value - " " - A + the - Show Results: $ a ready to eat steamed buns! the B ready to eat steamed buns! the Lao Zi starts to eat steamed bun to pull! theMade a bun, divided in 2 and a half, one half#Task 1 theThe bun [0] came. Eaten by [A]!#Task 2 -The bun [0] came. Eaten by [B]!#Task 3 in made a bun, divided in 2 and a half, one half thesteamed Bun [1] Come on. Eaten by [A]! thesteamed Bun [1] Come on. Eaten by [B]! About made a bun, divided in 2 and a half, one half thesteamed Bun [2] Come on. Eaten by [A]! thesteamed Bun [2] Come on. Eaten by [B]! the made a bun, divided in 2 and a half, one half +steamed Bun [3] Come on. Eaten by [A]! -steamed Bun [3] Come on. Eaten by [B]! the made a bun, divided in 2 and a half, one halfBayisteamed Bun [4] Come on. Eaten by [A]! thesteamed Bun [4] Come on. Eaten by [B]! the made a bun, divided in 2 and a half, one half -steamed Bun [5] Come on. Eaten by [A]! -steamed Bun [5] Come on. Eaten by [B]! the made a bun, divided in 2 and a half, one half theSteamed Bun [6] Come on. Eaten by [A]! theSteamed Bun [6] Come on. Eaten by [B]! the made a bun, divided in 2 and a half, one half -steamed Bun [7] Come on. Eaten by [A]! thesteamed Bun [7] Come on. Eaten by [B]! the made a bun, divided in 2 and a half, one half thesteamed Bun [8] Come on. Eaten by [A]! 94steamed Bun [8] Come on. Eaten by [B]! the made a bun, divided in 2 and a half, one half thesteamed Bun [9] Come on. Eaten by [A]! theThe bun [9] came. Eaten by [B]!
example of a co-process
Concurrency effect with generator yield implementation for single-threaded implementations (embryonic of asynchronous IO)