异步的问题:

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())

同样的代码,在VSCODE中就不报错,在咱们这就报错:Traceback (most recent call last): File “<string>”, line 1248, in init_ctx File “<string>”, line 62, in <module> TypeError: Object of type coroutine is not JSON serializable sys:1: RuntimeWarning: coroutine ‘main’ was never awaited RuntimeWarning: Enable tracemalloc to get the object allocation traceback 请问是什么原因呢?想取得所有合约货币对名称 谢谢


More

ChaoZhang "TypeError: Object of type coroutine is not JSON serializable":这个错误表示您尝试将一个类型为 coroutine 的对象序列化为 JSON。您可能需要先使用 await 运行 coroutine,然后序列化它的结果。 "RuntimeWarning: coroutine 'main' was never awaited":这个警告表示 coroutine 函数 'main' 被调用了,但从未被 await 运行,这意味着该函数还没有被异步执行。为了解决这个问题,您应该在调用 coroutine 函数时使用 await 关键字,或使用适当的异步方法运行它。