Coroutine within a Thread gives RunTime Error for Websocket Connection

61 Views Asked by At

I have the following code snippet:

import threading
import asyncio
from binance import AsyncClient, BinanceSocketManager

class TestThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        asyncio.run(main())
    
    async def main():
        # where the problem is:
        client = await AsyncClient.create()
        bm = BinanceSocketManager(client)

        # optional:
        await asyncio.sleep(1)

if __name__ == '__main__':
    thread1 = TestThread()
    thread1.start()

The idea is to connect the websocket as a coroutine and thread other operations while the connection remains. I want to separately operate within a module. So I placed a coroutine within a thread. I am using WebSocket Documentation for python-binance api. (The code for websocket works fine outside the thread.) But when I add AsyncClient.create() I recieve RuntimeError("can't register atexit after shutdown")

I don't know where the problem comes from.

0

There are 0 best solutions below