我正在尝试执行一个将在python中激活冷却计时器的命令,但是我遇到了问题。它将整个程序锁定为在冷却完成之前不再允许任何其他输入。我希望能够输入更多命令,同时循环等待X秒钟的时间过去。
示例:
import asyncio
import time
count = 0
cool = 0
async def main():
global count
while True:
userinput = input("enter command")
if userinput == "test":
count = count + 1
print("did test: " + str(count) + " times.")
if count >= 3:
await cooldown())
while count >= 3:
if cool == 0:
count = 0
else:
print("Command still on cooldown.")
else:
print("not a real command")
async def cooldown():
global cool
cool = 1
print("Cooldown is activated.")
time.sleep(3)
cool = 0
print("Cooldown is deactivated.")
asyncio.run(main())
尽管我不太了解Java或Python中的线程,但我认为这可以帮助您解决问题。
https://realpython.com/intro-to-python-threading
线程允许您同时运行多个功能,例如,一个线程可以接受键盘输入,而另一个线程可以将对象绘制到屏幕上。至少这是我的理解。