[Python] Task exception was never retrieved and bound to a different event loop, python asyncio

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm writing code, and I get these errors:

    • Task exception was never retrieved
    • bound to a different event loop, python asyncio.

    Here is the code (I shortened part of the code, since the system did not allow it, but left the main points):

    async def TickersChannel_ws(timeframe_):

    """ Websoket подписка на цены"""
    url = "wss://ws.okx.com:8443/ws/v5/business"
    async with websockets.connect(url) as ws:

    subs = {
    "op": "subscribe",
    "args": [
    dict(channel="mark-price-candle" + timeframe_ + "m", instId="SOL-USDT-SWAP")
    ]
    }

    await ws.send(json.dumps(subs))

    async for msg in ws:
    msg = json.loads(msg)
    if "event" not in msg:
    yield msg.get("data")[0][0]


    async def producer(que_):
    async for ticker in TickersChannel_ws(timeframe):
    await que_.put(ticker)
    await asyncio.sleep(0.1)


    async def consumer_algoritm(que_, coin_, volume_):

    global ts

    while True:
    ts_ = await que_.get()

    if ts != ts_:
    ts = ts_

    await asyncio.sleep(0.1)


    """ Основной код """

    if __name__ == "__main__":

    print(f'Начали в = {datetime.datetime.today().strftime("%Y-%m-%d-%H.%M.%S")}, стартовый баланс = {USDT_balance()}')
    print("----------------------------------------")
    cancel_order(get_list_order()[1], coin)
    queue = asyncio.Queue(1)
    ts = candleTelo(coin, timeframe)

    while True:

    try:
    async def main1():
    task1 = asyncio.create_task(producer(queue))
    task2 = asyncio.create_task(consumer_algoritm(queue, coin, volume))

    await task1
    await task2

    asyncio.run(main1())

    except Exception as e:
    continue


    According to the traceback I think it's about ts_ = await que_.get() in this string, but what to do with it?

    Traceback is here:

    Task exception was never retrieved
    future: <Task finished name='Task-297' coro=<consumer_algoritm() done, defined at C:\Users\Кирилл и Любимка\PycharmProjects\OKX_bot\test\bot_razmahPredel_soket.py:159> exception=RuntimeError('<Queue at 0x1f5049b92b0 maxsize=1 tasks=5813> is bound to a different event loop')>
    Traceback (most recent call last):
    File "C:\Users\Кирилл и Любимка\PycharmProjects\OKX_bot\test\bot_razmahPredel_soket.py", line 164, in consumer_algoritm
    ts_ = await que_.get()
    ^^^^^^^^^^^^^^^^
    File "F:\Python\Lib\asyncio\queues.py", line 183, in get
    getter = self._get_loop().create_future()
    ~~~~~~~~~~~~~~^^
    File "F:\Python\Lib\asyncio\mixins.py", line 20, in _get_loop
    raise RuntimeError(f'{self!r} is bound to a different event loop')
    RuntimeError: <Queue at 0x1f5049b92b0 maxsize=1 tasks=5813> is bound to a different event loop

    Continue reading...

Compartilhe esta Página