我嘗試使用time.sleep(),但它沒有等待變數,而是暫停了整個代碼,不允許變數更改。這是我的代碼:
@tasks.loop(seconds=5.0) #This is the code that changes the variable (Before this block of code, the variable fedex is defined)
global check, fedex
fedex = int(fedex) r.randint(-500,500)
if(check=="0"):
check = "1"
else:
check = "0"
@client.command() #This is the code in which, after the variable changes, the bot should type if the variable fedex went up or down.
async def bet(ctx, arg1):
def new():
global current
if(arg1=="fedex"):
global fedex
current = fedex
else:
return
new()
stok = current
if(check=="0"):
while(check != "1"):
time.sleep(1.0)
elif(check=="1"):
while(check != "0"):
time.sleep(1.0)
new()
stok2 = current
if(stok2>stok):
await ctx.send("It went up!")
else:
await ctx.send("It went down.")
我不知道用什么來代替time.sleep(1.0)。
uj5u.com熱心網友回復:
你可以await asyncio.sleep(1)改用。Asyncio.sleep像 一樣作業time.sleep,但它不會阻止整個代碼執行。它只停止一個事件。同時,事件回圈中的其他任務將繼續運行。雖然使用time.sleep您的整個代碼不會做任何其他事情。但是,asyncio.sleep在您的部分代碼“休眠”時要求事件回圈執行其他任務。
用例子很好的解釋
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/399631.html
