Python co-process functions

Source: Internet
Author: User
Tags generator

The co-function is a generator that uses the yield expression form

< Span class= "Hljs-keyword" >def eater print ( " %s eat food " %name) while true:food = yield print ( "Done") G = Eater ( "Gangdan") print (g)               

Generator Object eater at 0x00000000028dc048
This proves that G is now the generator function.

1.2 function Assignment Process

Using the expression form of yield

to run next (), let the function initialize and stop at yield, equivalent to the initialization function, and then send (), send will pass a value to yield
* * Next () and send () all allow the function to continue running at the last paused position.

Next is to have the function initialize

Send assigns a value to yield when the next execution of the code is triggered

Def Eater(name):Print‘%sStart to eat food '%name) food_list=[]WhileTrue:food=Yield food_listPrint‘%s get %s< Span class= "hljs-string", to-start eat ' % (Name,food)) food_list.append (food) E= Eater ( ' steel egg ') # wrapper (") # print (e) print (next (e)) # is now a run function that initializes the function to print (e.send (< Span class= "hljs-string" > ' bun ')) #print (e.send (print (e.send ( ' garlic bun ')              

The key here is:
To run the next () function first

Use the adorner function to run the next () function first:

Def Start(func):Def Wrapper(*Args**Kwargs): Res=func (*args,**kwargs)# Next ()Next (RES)# That's the keyreturn resReturn wrapper@start # e = Start (e)Def Eater(name):Print‘%sStart to eat food '%name) food_list=[]WhileTrue:food=yield food_list print (  ' %s< Span class= "hljs-string" > get %s % (Name,food)) food_list.append (food) E= Eater ( ' steel egg ') # wrapper (' Steel egg ') print (e.send ( ' bun ')) print (e.send ( ' leek stuffing buns ') print (e.send ( ' garlic bun ')              

After @start # e = Start (e) is written, run the start function, and the start function returns wrapper

1.3 The process function is initialized with the adorner

The main purpose here is to prevent forgetting to initialize the next operation, adding in the adorner

Def Init(func):Def Wrapper(*Args**Kwargs): Res= Func (*args,**kwargs)Next (RES)# to execute next herereturn resReturn wrapper@init # Eater=init (rater)Def Eater(name):Print"%sEat food "%name) food_list=[]WhileTrue:food=Yield food_listPrint"%s star to eat %s "% ( Name,food) food_list.append print ( "Done") g Span class= "OP" >= Eater ( "Gangdan")  # there is no need for Nextprint (g.send ( "1")) print (g.send ( "2")) print (g.send ( Span class= "St" > "3")) print (g.send ( "4")              

Gangdan Eat food
Gangdan Star to eat 1
[' 1 ']
Gangdan Star to eat 2
[' 1 ', ' 2 ']
Gangdan Star to eat 3
[' 1 ', ' 2 ', ' 3 ']
Gangdan Star to eat 4
[' 1 ', ' 2 ', ' 3 ', ' 4 ']

Just use the adorner function, write the adorner function immediately, write @init immediately think of * * Eater=init (EATER) * *, first execute the adorner function.

The key is next (res), and here is the initialization of the generator. This will be done only once, and after the execution, the E.send () is running behind.

Reprinted from:

Python co-process functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.