Python---await/async keyword

Source: Internet
Author: User

Tag: TPs Wait None blank method multiple lis for Def

Tweet: Play the await/async of Python 3.5

First look at the normal execution between the two functions
def func1 (): Print ("func1 Start") Print ("Func1 End") def func2 (): Print ("Func2 Start") Print ("Func2 a") Print ("Func2 b") Print ("FUNC2 C") Print ("Func2 End") func1 () Func2 ()
Func1 startfunc1 endfunc2 startfunc2 afunc2 bfunc2 Cfunc2 End

The interaction between the two functions cannot be implemented.

Set the two functions to be a co-process, plus the ASYNC keyword
Asyncdef func1 (): Print ("func1 Start") Print ("Func1 End")Asyncdef func2 (): Print ("Func2 Start") Print ("Func2 a") Print ("Func2 b") Print ("FUNC2 C") Print ("Func2 End") F1=func1 () F2=Func2 () print (F1,F2)
Object 0x000000000084c468 Object  0x000000000084cd58>sys:1'func1' was never awaited #警告信息sys:1'func2' was never awaited

# The above is no longer just a function, but a co-process object, which is not executed when called
So why do you have a co-process object? How exactly does the code execute?

The key is that the process is really very similar to the Python generator, and there is a Send method. We can initiate execution of a process by invoking the Send method.

 F1 = func1 () F2  = Func2 ()  try  : Print (  f1.send   " ) f1.send (None) except stopiteration  as   e:  Span style= "COLOR: #ff0000" > #这里也是需要去捕获StopIteration方法   pass  try  : Print (  "  F1.send   " ) f2.send (None) except Stopiteration  as   E:pass  

A stopiteration exception is a mechanism by which a markup generator (or a co-process like this one) finishes executing. Although this is an exception, it is really what we expect! We can wrap it up with the appropriate Try-catch code so that we can avoid the error message.

Although the above two processes are executed, but did not do the alternate execution, and the original is not different, and seems more troublesome

A thread similar to threads can be performed alternately between multiple threads, but unlike threads, where the transitions between the processes are explicit, the threads are implicitly (in most cases, better). So we need to add the explicit switch code.

We may speculate on this scenario based on the use of the generator or the gevent: ( It is wrong )

Async def func1 ():    print ("func1 start")    yield      Print ("func1 end")

Yield is not allowed in local coprocessor functions, but as a replacement we can use the await expression to pause the execution of the process. Note the await _something_, where the _something_ represents a generator-based co-object, or a special future-like object.

The following uses a generator-based co-process function
Import Types@types.coroutinedefSwitch():    yieldAsyncdef func1 (): Print ("func1 Start")    await Switch() print ("Func1 End")Asyncdef func2 (): Print ("Func2 Start") Print ("Func2 a") Print ("Func2 b") Print ("FUNC2 C") Print ("Func2 End") F1=func1 () F2=Func2 ()Try: F1.send (None) except Stopiteration asE:passTry: F2.send (None) except Stopiteration asE:pass
Func1 startfunc2 startfunc2 afunc2 bfunc2 Cfunc2 End

Note the above output: just perform the conversion of the function, there is func1 conversion to FUNC2, and did not go back to func1. So how do you get it back in func1?

We can use the Send method again for the FUNC1 process, based on the generator, again activating yield down execution
Import Types@types.coroutinedefSwitch():    yieldAsyncdef func1 (): Print ("func1 Start")    await Switch() print ("Func1 End")Asyncdef func2 (): Print ("Func2 Start") Print ("Func2 a") Print ("Func2 b") Print ("FUNC2 C") Print ("Func2 End") F1=func1 () F2=Func2 ()Try: F1.send (None) except Stopiteration asE:passTry: F2.send (None) except Stopiteration asE:passTry: F1.send (None) except Stopiteration asE:pass
Func1 startfunc2 startfunc2 afunc2 bfunc2 cfunc2 endfunc1 End

The above can be realized two functions alternating once, back to the original process. However, we would prefer a continuous invocation of send to drive a different coprocessor to execute until send throws a Stopiteration exception.

For this we

Create a new function that passes through a list of threads, and the function executes the threads until it's all over. All we have to do now is call this function.
Import Types@types.coroutinedefSwitch():    yieldAsyncdef func1 (): Print ("func1 Start")    await Switch() print ("func1 e")    await Switch() print ("Func1 End")Asyncdef func2 (): Print ("Func2 Start") Print ("Func2 a")    await Switch() print ("Func2 b") Print ("FUNC2 C")    await Switch() print ("Func2 End") def run (task_list): Coro_list=list (task_list) whilecoro_list: forCoroinchlist (coro_list):Try: Coro.send (None) except StopIteration:coro_list.remove (Coro) F1=func1 () F2= Func2 ()

run ([f1,f2])

Python---await/async keyword

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: info-contact@alibabacloud.com 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.