Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement asyncio Asynchronous Protocol in python

Shulou Source: shulou.com Published: 2022-05-31 19:13:00 09月14日 Update

Most people do not understand the knowledge points of this article "how to achieve asyncio Asynchronous Cooperation in python", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve asyncio Asynchronous Cooperation in python" article.

First, define the collaborative process

The tasks performed by asyncio are called collaborators, but Asyncio does not bring true parallelism

The multithreading of Python cannot bring true parallelism because of the existence of GIL (global interpreter lock).

Import asyncio# defines a co-program async def task (): print ('this is a co-program') # to determine whether it is a co-program through async Return Trueprint (asyncio.iscoroutinefunction (task)) 2. Run import asyncio# to define a co-program async def task (s) through async: print ('please wait {} seconds' .format (s)) await asyncio.sleep (s) print ('co-program ends') # co-program running loop = asyncio.get_event_loop () loop.run_until_complete (task (3)) 3. Co-program callback

After joining us to deal with the collaborative task, we need to tell the developer that the program is over here.

You need to use co-program callback.

Import asyncio# defines an async def task (s) through async: print ('Please wait {} seconds' .format (s)) await asyncio.sleep (s) return 'where task is over Others go on 'def callback (future): print (future.result ()) future = asyncio.ensure_future (task (3)) future.add_done_callback (callback) loop = asyncio.get_event_loop () loop.run_until_complete (future) # the result is as follows: # Please wait 3 seconds # here task is over, others continue. 4. Run multiple collaborations.

There are often multiple collaborators in the same project

Need to run with the help of asyncio.gather function

Import asyncio# defines a protocol async def task1 (s) through async: print ('Please wait {} seconds' .format (s)) await asyncio.sleep (s) print ('here task1 is over') # define a protocol async def task2 (s) through async: print ('please wait {} seconds' .format (s) await asyncio.sleep (s) print ('here task2 ends') # run method-loop = asyncio .get event_loop () loop.run_until_complete (asyncio.gather (task1 (1)) Task2 (3) # run method II Coros = [task1 (1), task1 (3)] loop.run_until_complete (asyncio.gather (* coros)) # the result is as follows: # Please wait 1 second # Please wait 3 seconds # here task1 ends # here task2 ends 5, run_forever

Run the cooperative program through run_until_complete. When the cooperative program is finished, the program ends and exits.

Running with run_forever, the program does not exit unless loop.stop () is called

Import asyncio# defines an async def task (s) through async: await asyncio.sleep (s) # program does not exit the end loop = asyncio.get_event_loop () asyncio.ensure_future (task (3)) loop.run_forever ()

If you want it to exit, you need to call loop.stop ()

We can call the

Import asyncio# defines an async def task (loop, s) through async: await asyncio.sleep (s) # turns off run_foreverloop.close () loop = asyncio.get_event_loop () asyncio.ensure_future (task (loop, 3)) loop.run_forever () VI. Close run_forever in multiple programs

A single co-program can be closed in a co-program, but if there are more than two co-programs

If a stop is completed first, it will cause other cooperative programs to exit abnormally, which is definitely not allowed.

So we can close it in the callback function.

Import asyncio, functoolsasync def task (x): await asyncio.sleep (x) print ('this is a cooperative task') def callback (loop): loop.stop () loop = asyncio.get_event_loop () future = asyncio.gather (task (1), task (3) future.add_done_callback (functools.partial (callback, loop)) loop.run_forever () these are the contents of the article "how to implement asyncio Asynchronous Protocol in python". I believe you all have some understanding. I hope the content shared by the editor will be helpful to all of you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Run content program task function multiple article method knowledge article result this is Zhongguan two personnel value overall join us single can be passed through Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Microsoft Huawei Shulou Technology Xiaomi