Python 在旧异步函数仍在运行时启动新异步函数
all函数创建没有可用股票的股票列表。然后,它运行一个循环函数alert,在股票可用时提醒我。我需要同时运行所有这些alert循环,但是all在开始下一个循环之前等待第一个库存可用 我尝试使用线程为每个股票创建一个线程,但我不能等待线程。start()Python 在旧异步函数仍在运行时启动新异步函数,python,asynchronous,discord.py,discord.py-rewrite,Python,Asynchronous,Discord.py,Discord.py Rewrite,all函数创建没有可用股票的股票列表。然后,它运行一个循环函数alert,在股票可用时提醒我。我需要同时运行所有这些alert循环,但是all在开始下一个循环之前等待第一个库存可用 我尝试使用线程为每个股票创建一个线程,但我不能等待线程。start() stocks=[“stocks”]您可以为事件循环安排任务,而不必立即等待它们。下面是一个使用 您可以为事件循环安排任务,而无需立即等待。下面是一个使用 这正是我需要的!非常感谢。这正是我需要的!非常感谢。 async def all(self,
stocks=[“stocks”]您可以为事件循环安排任务,而不必立即等待它们。下面是一个使用
您可以为事件循环安排任务,而无需立即等待。下面是一个使用
这正是我需要的!非常感谢。这正是我需要的!非常感谢。
async def all(self, ctx):
stocks = requests.get(f'https://api.torn.com/torn/?
selections=stocks&key={api}').json()['stocks']
zero = []
acronymz = []
for items in stocks:
if stocks[items]['available_shares'] == 0:
zero.append(items)
acronymz.append(stocks[items]['acronym'])
await ctx.send(f'Zero: {zero}')
for acronyms in zero:
print(acronyms)
# Thread(target=alert, args=(ctx, acronyms)).start()
await alert(ctx, acronyms)
# await asyncio.sleep(0.5)
async def alert(ctx, items):
stocks = requests.get(f'https://api.torn.com/torn/?selections=stocks&key={api}').json()['stocks'][items]
if stocks['available_shares'] == 0:
await ctx.send(f'I am now watching {stocks["acronym"]}. I will let you know when there are shares available!')
while stocks['available_shares'] == 0:
stocks = requests.get(f'https://api.torn.com/torn/?selections=stocks&key={api}').json()['stocks'][items]
print(stocks)
await asyncio.sleep(5)
await ctx.send(f'There are {stocks["available_shares"]} in {stocks["acronym"]}')
await asyncio.gather(*(alert(ctx, acronyms) for acronyms in zero))