我正在制作一个货币模拟器,我想使用asyncio.sleep((在向帐户添加更多货币之前进行延迟。问题是,多个人将同时使用,所以我需要一种方法使其仅暂停一个人。这或多或少是我的代码:
time = True
if not time:
print("wait more")
return
# add money and stuff here
time = False
await asyncio.sleep(10.0)
time = True
提前感谢!!!
一种可能的解决方案可以是:
class character:
...
def startPause(self,pause=10)# pause is how long the pause will be and is at default set to 10
self.startTime = time.time()
self.pauseTime = pause
def add_money(self):
if time.time-self.startTime>self.pauseTime:
print("wait more")
...
这个程序通过调用startPause函数来设置startTime。除非startTime-time.time大于暂停的时间,否则它将中断/中断函数。