Python 3.x 使用Python';s异步
我试图用asyncio同时启动多个任务,但我发现这些任务实际上并没有并发运行。有些东西我一定会错过的。我对asyncio的了解正在增长,但我仍处于学习阶段。我正在使用asyncio.Queue()在类之间传输数据。 下面是一些代码: main.pyPython 3.x 使用Python';s异步,python-3.x,python-asyncio,Python 3.x,Python Asyncio,我试图用asyncio同时启动多个任务,但我发现这些任务实际上并没有并发运行。有些东西我一定会错过的。我对asyncio的了解正在增长,但我仍处于学习阶段。我正在使用asyncio.Queue()在类之间传输数据。 下面是一些代码: main.py async def main(): ws = WebSocket() await ws.startServer(ip, port) # websocket's send and receive handlers should wor
async def main():
ws = WebSocket()
await ws.startServer(ip, port) # websocket's send and receive handlers should work concurently
await ws.websocketMsgs.otherTask # this task should also run concurrently with the two above
asyncio.run(main(), debug=False)
webSocket.py
from webSocketMsgs import WebSocketMsgs
class WebSocket:
def __init__(self):
self.server = None
self.sendTask = None
self.receiveTask = None
self.websocketMsgs = WebSocketMsgs()
async def startServer(self, ipAddress, port):
self.server = await websockets.serve(lambda websocket, path : self.msgHandler(websocket, path), ipAddress, port)
await self.server.wait_closed()
async def msgHandler(self, websocket, path):
self.sendTask = asyncio.create_task(self.sendHandler(websocket, path))
self.receiveTask = asyncio.create_task(self.receiveHandler(websocket, path))
await asyncio.wait([self.sendTask, self.receiveTask], return_when = asyncio.ALL_COMPLETED)
async def sendHandler(self, websocket, path):
while True:
itemToSend = await self.websocketMsgs.qOutgoingData.get()
await websocket.send(itemToSend)
self.websocketMsgs.qOutgoingData.task_done()
async def receiveHandler(self, websocket, path):
async for message in websocket:
await self.websocketMsgs.processIncomingMsgs(message)
webSocketMsgs.py
class WebSocketMsgs:
def __init__(self):
# An asyncio.Queue() that is populated in an other class
self.qIncomingSensorData = self.someclass.qIncomingSensorData
self.qOutgoingData = asyncio.Queue()
self.otherTask = asyncio.create_task(self.consumeSensorData())
async def processIncomingMsgs(self, msg):
# Do stuff with messages coming from the websocket
async def consumeSensorData(self):
while True:
msg = await self.qIncomingSensorData.get() # Data to be processed and sent out via websocket
# Some logic
self.qOutgoingData.put_nowait(formattedMsg)
我认为:
- 只要我通过websocket发送消息,
就会做出反应receiveHandler
正在运行,并每隔1秒用一个元素填充队列otherTask
- 如果没有通过websocket接收到消息,则
将保留并每20秒发送20条消息。如果我通过websocket发送虚拟消息,它将“解锁”sendHandler任务,并发送x秒内累积的消息量sendHandler
有人能解释我为什么会观察到这种行为,并建议一种方法来安排我的代码使所有内容同时运行吗?只需在另一个线程中运行的回调中使用
调用\u soon\u threadsafe(self.qIncomingSensorData.put\u nowait,msg)
。只需使用调用\u soon\u threadsafe即可(self.qIncomingSensorData.put_nowait,msg)
在另一个线程中运行的回调中。谁填充了qIncomingSensorData
,以及如何填充?Hi-Hrvoje:)是上一篇文章[link]()中的receivedMsgCallback,现在也使用调用\u soon\u threadsafe()
。我使用self.qIncomingSensorData.put_nowait(msg)在回调中。非常感谢你的支持嗨!如果你使用call_soon\u threadsafe
,那么它为什么会卡住就真的不清楚了。谁填充了qIncomingSensorData
,以及如何填充?Hi-Hrvoje:)这是上一篇文章[link]()中的ReceivedMgCallback,现在也使用了call_soon\u threadsafe()
。我使用self.qIncomingSensorData.put_nowait(msg)在回调中。非常感谢你的支持嗨!如果使用call\u soon\u threadsafe
,则不清楚它为什么会卡住。