0

The following code works, but fails to exit upon CTRL+c

Can anyone explain why, and how to fix?

It's asyncio code, but I'm using trio-asyncio (forward planning) to allow me to also use trio code.

#!.venv/bin/python

import asyncio

import trio
import trio_asyncio


from binance import AsyncClient, BinanceSocketManager

as_trio = trio_asyncio.aio_as_trio
as_aio = trio_asyncio.trio_as_aio

from contextlib import asynccontextmanager

@asynccontextmanager
async def AClient():
    client = await AsyncClient.create()
    try:
        yield client
    except KeyboardInterrupt:
        print('ctrl+c')
        exit(0)
    finally:
        await client.close_connection()

async def kline_listener(client):
    print(1)
    bm = BinanceSocketManager(client)
    async with bm.kline_socket(symbol='BNBBTC') as stream:
        while True:
            try:
                res = await stream.recv()
                print(res)
            except KeyboardInterrupt:
                break
@as_trio
async def aio_main():
    async with AClient() as client:
        exchange_info = await client.get_exchange_info()
        tickers = await client.get_all_tickers()
        print(client, exchange_info.keys(), tickers[:2])

        kline = asyncio.create_task(kline_listener(client))
        await kline

if __name__ == "__main__":
    trio_asyncio.run(aio_main)

I've placed except KeyboardInterrupt at 2 places, though it appears to do nothing.

P i
  • 29,020
  • 36
  • 159
  • 267

0 Answers0