将AioRTC数据通道划分为多个线程



我有一个双向数据通道设置,它从浏览器客户端接收心跳,并在心跳保持期间保持会话活动。心跳是WebRTC的"主要"通信,但我有其他需要不断发送的信息(如坐标)。

当提供webtc offer时,它接受HTTP请求:

  1. 创建一个新的事件循环'rtcloop'
  2. 设置为主事件循环。
  3. 然后运行'rtcloop'直到完成,调用我的webRtcStart函数并传递会话信息。
  4. 然后运行一个新的线程,目标是' rtloop ',永远运行它并启动。
  5. 在新线程中,我用'get_event_loop'设置循环,然后定义' @webRtcPeer.on(" Datachannel ")',所以当我们得到一个Datachannel消息时,我们运行代码。根据情况,我尝试做以下操作:
ptzcoords = 'Supported' #PTZ Coords will be part of WebRTC Communication, send every 0.5 seconds.
ptzloop = asyncio.new_event_loop()
ptzloop.run_until_complete(updatePTZReadOut(webRtcPeer, cameraName, loop))
ptzUpdateThread = Thread(target=ptzloop.run_forever)
ptzUpdateThread.start()

无论我如何构造东西,我得到的恒定错误是"协程'updatePTZReadOut'从未等待过">

updatePTZReadOut为:

async def updatePTZReadOut(rtcPeer, cameraName, eventLoop):
# Get Camera Info
# THE CURRENT ISSUE I am having is with the event loops, because this get's called to run in another thread, but it still needs
# to be awaitable, 
# Current Warning Is: /usr/lib/python3.10/threading.py:953: RuntimeWarning: coroutine 'updatePTZReadOut' was never awaited
# Ref Article: https://xinhuang.github.io/posts/2017-07-31-common-mistakes-using-python3-asyncio.html
# https://lucumr.pocoo.org/2016/10/30/i-dont-understand-asyncio/

# Get current loop
# try:
loop = asyncio.set_event_loop(eventLoop)
#     loop.run_until_complete()
# except RuntimeError:
#     loop = asyncio.new_event_loop()
#     asyncio.set_event_loop(loop)

# Getting Current COORDS from camera
myCursor.execute("Select * from localcameras where name = '{0}' ".format(cameraName))
camtuple = myCursor.fetchall()
camdata = camtuple[0]
# Create channel object
channel_local = rtcPeer.createDataChannel("chat")
while True:
ptzcoords = readPTZCoords(camdata[1], camdata[3], cryptocode.decrypt(str(camdata[4]), passwordRandomKey))
print("Updating Coords to {0}".format(ptzcoords))
# Publish Here
await channel_local.send("TTTT")
asyncio.sleep(0.5)

有什么帮助吗?

updatePTZReadOut是异步函数。当你调用这个函数时,你需要添加await。

最新更新