【发布时间】:2021-04-06 12:27:16
【问题描述】:
我正在研究如何在asyncio 中返回一个列表[]
我知道asyncio.gather 可以帮助我,但是我现在很困惑有很多方法。
如何从 main() 返回值?谢谢
async def wait_until(dt):
# sleep until the specified datetime
now = datetime.now()
await asyncio.sleep((dt - now).total_seconds())
async def run_at(dt, coro):
await wait_until(dt)
return await coro
async def main():
test=[]
async for message in client.iter_messages(channel):
test.append(message)
return test
loop = asyncio.get_event_loop()
loop.create_task(run_at(datetime(2020, 12, 29, 19, 17),main()))
loop.run_until_complete(asyncio.gather(*[main()]))
# How to get test[] or How to pass it to another task?
loop.run_forever()
【问题讨论】:
-
我将使用全局变量,但我确信有更好的方法来做到这一点。另外,您是否知道在第一个循环后返回
test[]?
标签: python async-await return python-asyncio telethon