Seperating AioRTC datachannel into multiple threads

380 views Asked by At

I have a two-way datachannel setup that takes a heartbeat from a browser client and keeps the session alive as long as the heartbeat stays. The heartbeat is the 'main' communication for WebRTC, but I have other bits of into (Such as coordinates) I need to send constantly.

To do this when a webrtc offer is given, it takes that HTTP request:

  1. Creates a new event loop 'rtcloop'
  2. Set's that as the main event loop.
  3. Then run 'rtcloop' until complete, calling my webRtcStart function and passing through the session info.
  4. Then run a new thread with the target being 'rtcloop', run it forever and start.
  5. Inside the new thread I set the loop with 'get_event_loop' and later define ' @webRtcPeer.on("datachannel")' so when we get a Datachannel message, we run code around that. Depending on the situation, I attempt to do the following:
                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()

The constant error I get no matter how I structure things is "coroutine 'updatePTZReadOut' was never awaited"

With updatePTZReadOut being:

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)

Any help here?

1

There are 1 answers

1
Sam On

updatePTZReadOut is async function. You need to add await whenever you call this function.