简体   繁体   English

如何在 asyncio python 中使用返回

[英]how to use return in asyncio python

import asyncio


def test_async_call(test):
    return test

def test_another_async_call(xyz):
    return xyz

async def first_one():
    return test_async_call('hello')
async def second_one():
    return test_another_async_call('hi')

async def final_one():
    tasks = first_one(), second_one()
    await asyncio.gather(*tasks)

I am pretty new to asyncio and trying to call multiple functions in async, is the above way not correct?我对 asyncio 很陌生,并尝试在 async 中调用多个函数,上述方法不正确吗? How can i call multiple functions in async way.如何以异步方式调用多个函数。

You can do the following (this is just a concept; this will not work how you want in your program)您可以执行以下操作(这只是一个概念;这不会在您的程序中按您想要的方式工作)

async def main():
    return "hi"

Then, start the async program by doing this:然后,通过执行以下操作启动异步程序:

loop = asyncio.get_event_loop()
result = loop.run_until_complete(main())

Now, result will be equal to "hi" .现在, result将等于"hi" The way I'd recommend starting further async functions is to simply do:我建议开始进一步的异步功能的方法是简单地执行以下操作:

variablename = await asyncfunction(param)

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM