so i am trying to build a script which streams BTC data using Binance's API, similar to this. The issue however is that, when testing in an .ipynb cell, the following code works perfectly:
import asyncio
import nest_asyncio
from binance.client import Client
from binance import BinanceSocketManager
nest_asyncio.apply()
async def main():
client = Client()
bsm = BinanceSocketManager(client)
socket = bsm.trade_socket('BTCUSDT')
async with socket as ts:
while True:
print('before await')
msg = await ts.recv()
print(msg)
# ---
# loop = asyncio.new_event_loop()
# loop.run_until_complete(main())
asyncio.run(main())
However, when i place this code into a .py file, and try executing it, i can only reach the first print('before await') instruction, and no data is shown.
I am developing inside a .devcontainer for both the .ipynb file and the .py file, and both have the same Python and library versions installed.
I have tried using different instructions, such as running the async function with
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
# instead of
asyncio.run(main())
However in both cases it works perfectly fine in the .ipynb file but not in the .py one.
I would greatly appreciate any help, thanks! :)
Works like a charm for me on Windows 10.
Copied your code, installed the modules.
Using python virtual environment.
The output until CTRL-C