如何收集不同数量的腐蚀物?

2024-06-25 23:27:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写一个程序,在某个时候需要收集未知数量的腐蚀物,这个程序管理多个账户,每个账户都有一个客户腐蚀物,我如何收集未知数量的客户?在

这是我当前的聚集函数。在

loop.run_until_complete(asyncio.gather(
    main_client.start(token),
    account1.client.start(account.credentials),
    #More accounts should go here
    main_player_control.loop()
    #If possible, also would like to have multiple player controls
))

Tags: 函数run程序clientloopasyncio数量客户
1条回答
网友
1楼 · 发布于 2024-06-25 23:27:16

作为@文森特said,您可以使用*函数调用语法向asyncio.gather传递数量可变的协同路由。或者,您可以调用asyncio.wait,它接受一个列表。在

看一下代码,不清楚单个gather是否正确。gather一次启动所有协同程序,即与主循环并行运行授权协同程序。可能需要先进行某种初始化/授权,然后再进行控制循环。在这种情况下,这样的main协同程序会更好地为您服务:

async def main(main_client, token, accounts, main_player):
    # authorize the main client
    await main_client.start(token),
    # authorize all the accounts in parallel
    await asyncio.wait(account.client.start(account.credentials)
                       for account in accounts)
    # once the above is complete, start the main loop
    await main_player_control.loop()

loop.run_until_complete(main(main_client, token, accounts, main_player))

相关问题 更多 >