Python collaboration notes

One, CO range

A collaboration is a computer program component that generates subroutines for non-preemptive multitasking, allowing different entry points to pause or start executing programs at different locations

It can be seen as a function that can be pause and a generator.

Two, common package

asyncio,tornado,gevent

Three, the realization of the association.

yieldReturn

sendcall

Four, the four states of the association.

inspect.getgeneratorstate(…)Function confirms that this function returns one of the following characters:

GEN_CREATER:Wait for execution

GEN_RUNNING:The interpreter is executing.

GEN_SUSPENED:Pause at yield expression

GEN_CLOSED:end of execution

nextPre excitation (prime)

Case 1

 1 def My_Coroutines():
 2     print('-->start')
 3     x = yield
 4     print('x is %s' % x)
 5     print('-->end')
 6 
 7 g = My_Coroutines()
 8 next(g)
 9 
10 g.send('Q')

-->start
x is Q
-->end
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-1-dfd01338da14> in <module>()
      8 next(g)
      9 
---> 10 g.send('Q')

StopIteration:

out1

Preexcitation:

next(t) Or t.seed (None)

The program throws a StopIteration exception at the end of the program, because after Send sends the signal Q to the original function, the next yield is not found in the function, and then returns to Send to throw the exception. The solution can add yield at the end of the program

Case 2

 1 def My_Coroutines():
 2     print('-->start')
 3     x = yield
 4     print('x is %s' % x)
 5     print('-->end')
 6     yield
 7 
 8 g = My_Coroutines()
 9 next(g)
10 g.send('Q')

-->start
x is Q
-->end

out2

The order of implementation is:

The preexcitation function (next (g) – – & gt; print (‘- – gt; start’) – – & gt; pauses at the yield expression – & gt; the preexcitation execution ends – & gt; calls the original function (g. send (‘Q’) again(3) gt; Q’passes in X – & gt; print (‘x is% s’% x) – – & gt; print (‘- – gt; end’) – – & gt; pauses – & gt at the yield expression; program knotbeam

Case 3

 1 def My_Coroutines(a):
 2     print('-->start')
 3     print('a is {0}'.format(a))
 4     b = yield a
 5     print('a b is {0} {1}'.format(a,b))
 6     c = yield a + b
 7     print('a b c is {0} {1} {2}'.format(a,b,c))
 8     yield a + b + c
 9     
10 a = My_Coroutines(5)
11 
12 q1 = next(a)
13 print(q1)
14 q2 = a.send(3)
15 print(q2)
16 q3 = a.send(1)
17 print(q3)

-->start
a is 5
5
a b is 5 3
8
a b c is 5 3 1
9

out3

Five, yield from

  http://flupy.org/resources/yield-from.pdf

1.yield from

In order for the generator to be easily invoked directly in other functions, yield from is generated.

yield from That is to find the yield in the subgenerator from other functions.

yield fromCapture StopIteration exception from within

Six. Delegate generator

Generator function containing yield from < iterable> expression.

Case 4

 1 def htest():
 2     i = 1
 3     while i < 4:
 4         n = yield i
 5         if i == 3:
 6             return 100
 7         i += 1
 8 
 9 #Delegate generator10 def itest():
11     val = yield  from htest()
12     print(val)
13 
14 t = itest()
15 t.send(None)
16 j = 0
17 while j < 3:
18     j += 1
19     try:
20         t.send(j)
21     except StopIteration as e:
22         print('Abnormal')

adopt

Change yield from to yield

Change return to yield

Such changes can give a glimpse of yield from.

Leave a Reply

Your email address will not be published. Required fields are marked *