小编Dan*_*Dan的帖子

如何调用异步函数而不期望它们返回?

在下面的代码中,我想调用task1和task2,但不期望从这些方法返回,这可能吗?

import asyncio
async def say(something, delay):
  await asyncio.sleep(delay)
  print(something)

loop = asyncio.get_event_loop()
task1 = loop.create_task(say('hi', 1))
task2 = loop.create_task(say('hoi', 2))
loop.run_until_complete(asyncio.gather(task1, task2))
Run Code Online (Sandbox Code Playgroud)

我想处理在 while 循环中进入主队列的某些内容,而无需等待,因为我不需要返回函数,例如伪代码:

import asyncio
async def say(something, delay):
  await asyncio.sleep(delay)
  print(something)

def main():
    while True:
        # search for database news
        # call say asynchronous, but I do not need any return, I just want you to do anything, independent
        time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

parallel-processing asynchronous wait python-3.x python-asyncio

3
推荐指数
1
解决办法
4747
查看次数