The problem of asynchrony:

Author: lzhqlj, Created: 2023-03-17 22:48:45, Updated: 2023-03-17 22:49:07

import asyncio import aiohttp import websockets import json import pandas as pd from datetime import datetime

async def get_usdt_symbols(): url = “https://fapi.binance.com/fapi/v1/exchangeInfo” async with aiohttp.ClientSession() as session: async with session.get(url) as resp: response = await resp.json() symbols = [symbol_info[“symbol”] for symbol_info in response[“symbols”] if symbol_info[“quoteAsset”] == “USDT” and symbol_info[“contractType”] == “PERPETUAL”] return symbols

async def main(): symbol_list = await get_usdt_symbols() print(f"Total USDT perpetual symbols: {len(symbol_list)}")

# 在此处添加WebSocket连接和数据处理代码

if name == “main”: loop = asyncio.get_event_loop() loop.run_until_complete(main())

The same code, in VSCODE, does not return an error, in ours it does: Traceback (most recent call last): File , line 1248, ininit_ctxFile , line 62, in TypeError: Object of type coroutine is not JSON serializable sys:1: RuntimeWarning: coroutine What's the reason for that? I want to get all the contract currencies for the name.


More

ChaoZhang"TypeError: Object of type coroutine is not JSON serializable": This error indicates that you are attempting to serialize an object of type coroutine into JSON. You may need to run the coroutine with await first and then serialize its results. "RuntimeWarning: coroutine'main' was never awaited": This warning indicates that the coroutine function'main' was called but never executed by await, which means that the function has not yet been asynchronously executed. To resolve this issue, you should use the await keyword when calling the coroutine function, or run it using the appropriate asynchronous method.