在 Discord.py 中,是否可以等待变量从代码的不同部分更改而不是变量更改?

In Discord.py, is it possible to wait for a variable to change from a different part of the code than the variable changes?

我尝试使用 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)

您可以改用 await asyncio.sleep(1)Asyncio.sleep 的工作方式类似于 time.sleep,但它不会阻止整个代码执行。它只停止一个事件。同时,事件循环中的其他任务将继续 运行。使用 time.sleep 时,您的整个代码不会执行任何其他操作。但是,asyncio.sleep 要求事件循环在您的部分代码“休眠”时执行其他任务。