在 python 到 运行 两个无限 运行ning 函数中使用 asyncio

Using asyncio in python to run two infinitely running functions

我正在尝试同时 运行 两个无限循环函数,稍后会将其实现到连接到我的服务器的每个客户端的套接字聊天室应用程序中。问题是,每当我尝试收集的函数在无限循环中 运行 时,我的程序只会 运行 收集的第一个函数。

这是我的代码:


async def increment():

    global money

    while True:
        money += 1



async def displayMoney():

    global money

    while True:
        input(money)



async def main():

    global money

    await asyncio.gather(increment(), displayMoney())



asyncio.run(main())

我是异步编程的新手,抱歉。

如果在循环末尾添加 await asyncio.sleep(0),它允许循环给彼此时间 运行。但是,这意味着您不能像我试图做的那样 运行 任何停止主事件循环的东西,例如 time.sleep(1)input()。这很好,因为我不需要在我的主程序中使用任何这些,因为它使用 tkinter gui。