Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report