Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the ways for Python to get the return value of the co-program?

Shulou Source: shulou.com Published: 2022-06-02 06:22:58 10月06日 Update

This article mainly introduces "what are the ways in which Python obtains the return value of collaborative journey". In the daily operation, I believe that many people have doubts about the way in which Python obtains the return value of collaborative journey. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "what is the way in which Python obtains the return value of collaborative journey?" Next, please follow the editor to study!

Introduction

There are four ways to get the return value of a collaborative program:

1. Get it through ensure_future, which is essentially the result side of the future object

2. Use the create_task that comes with loop to get the returned value.

3. With callback, once the content in await is finished, callback will be run.

4. Use the partial module to pass values to the callback function

Source code import asynciofrom functools import partialasync def talk (name): print (f "talk function run.") Await asyncio.sleep (0.1) return f "{name} talk!" async def sleep (name): print (f "sleep function run.") Await asyncio.sleep (0.1) return f "{name} sleep!" def callback (name): print (f "active throw receive: {name}") if _ _ name__ = = "_ _ main__": # obtain via ensure_future Essentially, the result method in the future object # loop = asyncio.get_event_loop () # get_future1 = asyncio.ensure_future (talk ("Dog")) # get_future2 = asyncio.ensure_future (sleep ("Cat")) # loop.run_until_complete (get_future1) # loop.run_until_complete (get_future2) # print (get_future1.result ()) # print (get_future2.result ()) ) # using create_task that comes with loop Get the return value # loop = asyncio.get_event_loop () # task1 = loop.create_task (talk ("Dog")) # task2 = loop.create_task (sleep ("Cat")) # loop.run_until_complete (task1) # loop.run_until_complete (task2) # print (task1.result ()) # print (task2.result ()) # use callback once the content in await runs out Run callback # loop = asyncio.get_event_loop () # task1 = loop.create_task (talk ("Dog")) # task2 = loop.create_task (sleep ("Cat")) # task1.add_done_callback (callback) # task2.add_done_callback (callback) # loop.run_until_complete (task1) # loop.run_until_complete (task2) # print (task1.result ()) # print (task2.result ()) # use the partial module to pass the value loop = asyncio.get_event_loop () task1 = loop.create_task (talk ("Dog")) task2 = loop.create_task (talk ("Cat")) task1.add_done_callback (partial (callback)) task2.add_done_callback (partial (callback) loop.run_until_complete (task1) loop) to the callback function .run _ until_complete (task2) print (task1.result ()) print (task2.result ()) executes the results in turn

Getting through ensure_future is essentially the result method in the future object.

Use the create_task that comes with loop to get the return value

With callback, once the content in the await place is finished, callback will be run.

Use the partial module to pass values to the callback function

At this point, the study on "what are the ways in which Python can get the return value of the collaboration process" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Operation mode learning content function place object method essence module more help practical active next article source code theory knowledge article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Linux NVidia MariaDB