如何在 python 中多处理 async/await 函数?
How to multiprocess async/await functions in python?
我需要每 60 秒向我的所有汽车 运行 一个事件。我的下面代码的问题是 while 循环直到超时 (60) 才结束,因此只有汽车中的第一辆车是 运行.
class RunCars(BaseEvent):
def __init__(self):
interval_seconds = 60 # Set the interval for this event
super().__init__(interval_seconds)
# run() method will be called once every {interval_seconds} minutes
async def run(self, client, cars):
for car in cars:
channel = get_channel(client, "general")
await client.send_message(channel, 'Running this '+str(car))
await msg.add_reaction(str(get_emoji(':smiley:')))
reaction = None
while True:
if str(reaction) == str(get_emoji(':smiley:'))
await client.send_message(channel, 'Finished with this '+str(car))
try:
reaction, user = await client.wait_for('reaction_add', timeout=60, check=check)
except:
break
我尝试将代码更改为多线程进程,但在函数内部遇到了 async/await 问题,而且函数本身也出现了问题。
如有任何建议,我将不胜感激..
asyncio
模块允许您使用 gather
方法同时执行多个 async
方法。我认为你可以通过定义一个运行单辆车的方法来实现你想要的行为,然后用对 gather
的调用替换你的 for
循环,这将执行多个 run_one
协程(方法)同时:
import asyncio
class RunCars(BaseEvent):
def __init__(self):
interval_seconds = 60 # Set the interval for this event
super().__init__(interval_seconds)
async def run(self, client, cars):
coroutines = [self.run_one(client, car) for car in cars]
asyncio.gather(*coroutines)
async def run_one(self, client, car):
channel = get_channel(client, "general")
await client.send_message(channel, 'Running this '+str(car))
await msg.add_reaction(str(get_emoji(':smiley:')))
reaction = None
while True:
if str(reaction) == str(get_emoji(':smiley:'))
await client.send_message(channel, 'Finished with this '+str(car))
try:
reaction, user = await client.wait_for('reaction_add', timeout=60, check=check)
except:
break
一般来说,在编写异步代码时,您应该尝试用 gather
语句,以便它们同时执行。
我需要每 60 秒向我的所有汽车 运行 一个事件。我的下面代码的问题是 while 循环直到超时 (60) 才结束,因此只有汽车中的第一辆车是 运行.
class RunCars(BaseEvent):
def __init__(self):
interval_seconds = 60 # Set the interval for this event
super().__init__(interval_seconds)
# run() method will be called once every {interval_seconds} minutes
async def run(self, client, cars):
for car in cars:
channel = get_channel(client, "general")
await client.send_message(channel, 'Running this '+str(car))
await msg.add_reaction(str(get_emoji(':smiley:')))
reaction = None
while True:
if str(reaction) == str(get_emoji(':smiley:'))
await client.send_message(channel, 'Finished with this '+str(car))
try:
reaction, user = await client.wait_for('reaction_add', timeout=60, check=check)
except:
break
我尝试将代码更改为多线程进程,但在函数内部遇到了 async/await 问题,而且函数本身也出现了问题。
如有任何建议,我将不胜感激..
asyncio
模块允许您使用 gather
方法同时执行多个 async
方法。我认为你可以通过定义一个运行单辆车的方法来实现你想要的行为,然后用对 gather
的调用替换你的 for
循环,这将执行多个 run_one
协程(方法)同时:
import asyncio
class RunCars(BaseEvent):
def __init__(self):
interval_seconds = 60 # Set the interval for this event
super().__init__(interval_seconds)
async def run(self, client, cars):
coroutines = [self.run_one(client, car) for car in cars]
asyncio.gather(*coroutines)
async def run_one(self, client, car):
channel = get_channel(client, "general")
await client.send_message(channel, 'Running this '+str(car))
await msg.add_reaction(str(get_emoji(':smiley:')))
reaction = None
while True:
if str(reaction) == str(get_emoji(':smiley:'))
await client.send_message(channel, 'Finished with this '+str(car))
try:
reaction, user = await client.wait_for('reaction_add', timeout=60, check=check)
except:
break
一般来说,在编写异步代码时,您应该尝试用 gather
语句,以便它们同时执行。