在旧函数仍然存在时启动新的异步函数 运行
Starting a new async function while the old one is still running
all 函数创建一个没有可用股票的股票列表。然后它 运行s alert,一个循环函数,当股票可用时提醒我。我需要同时 运行 所有这些 alert 循环,但是 all 在开始下一个循环之前等待第一只股票可用。
我已经尝试使用线程为每只股票创建一个线程,但我无法等待 Thread.start()
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"]}')
股票 = https://pastebin.com/FhuR4d4R ["stocks"]
您可以为事件循环安排任务,而无需立即 await
ing 它们。这是一个使用 asyncio.gather
的示例
await asyncio.gather(*(alert(ctx, acronyms) for acronyms in zero))
all 函数创建一个没有可用股票的股票列表。然后它 运行s alert,一个循环函数,当股票可用时提醒我。我需要同时 运行 所有这些 alert 循环,但是 all 在开始下一个循环之前等待第一只股票可用。
我已经尝试使用线程为每只股票创建一个线程,但我无法等待 Thread.start()
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"]}')
股票 = https://pastebin.com/FhuR4d4R ["stocks"]
您可以为事件循环安排任务,而无需立即 await
ing 它们。这是一个使用 asyncio.gather
await asyncio.gather(*(alert(ctx, acronyms) for acronyms in zero))