sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.类型错误'未映射



执行代码时,会显示错误"sqlalchemy.orm. exe。UnmappedInstanceError:类的内置错误。TypeError'未映射">

engine = create_engine('mysql+pymysql://root:root@127.0.0.1/exchanges')
Session = sessionmaker(bind=engine)
session = Session()
async def response_to_sql(table):
for k1, v1 in data.items():
for t in table:
if table.__tablename__ == k1:     
for k2, v2 in v1.items():
ins = t(symbol=v2['symbol'], ask=v2['ask'], bid=v2['bid'])
return ins

async def handler(tables):
input_coroutines = [response_to_sql(table) for table in tables]
response = await asyncio.gather(*input_coroutines, return_exceptions=True)
session.add_all(response)
session.commit()
await session.close()
async def async_client(exchange):
client = getattr(ccxta, exchange)()
tickers = await client.fetch_tickers()
global data
data = {exchange:tickers}

await client.close()
return tickers
async def multi_tickers(exchanges):
input_coroutines = [async_client(exchange) for exchange in exchanges]
tickers = await asyncio.gather(*input_coroutines, return_exceptions=True)
a = print("async call spend:", time.time() - tic)
await handler(tables)
return tickers
if __name__ == '__main__':
exchanges = ['binance', 'bitget', 'bitmart', 'bitvavo', 'bybit', 'gate', 'huobi', 'kucoin', 'mexc', 'okx']
tables = [Binance, Bitget, Bitmart, Bitvavo, Bybit, Gate, Huobi, Kucoin, Mexc, Okx]
tic = time.time()
asyncio.run(multi_tickers(exchanges))

有人能告诉我是什么导致错误,我怎么能解决它?

谢谢。错误:

async call spend: 13.642300844192505
<sql.Gate object at 0x000001D4F0C12410>
Traceback (most recent call last):
File "c:UsersWendeDesktoparbitr.venvLibsite-packagessqlalchemyormsession.py", line 2636, in add
state = attributes.instance_state(instance)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '_sa_instance_state'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:UsersWendeDesktoparbitrmain.py", line 50, in <module>
asyncio.run(multi_tickers(exchanges))
File "c:UsersWendeAppDataLocalProgramsPythonPython311Libasynciorunners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "c:UsersWendeAppDataLocalProgramsPythonPython311Libasynciorunners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:UsersWendeAppDataLocalProgramsPythonPython311Libasynciobase_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "c:UsersWendeDesktoparbitrmain.py", line 43, in multi_tickers
await handler(tables)
File "c:UsersWendeDesktoparbitrmain.py", line 25, in handler
session.add_all(response)
File "c:UsersWendeDesktoparbitr.venvLibsite-packagessqlalchemyormsession.py", line 2663, in add_all
self.add(instance, _warn=False)
File "c:UsersWendeDesktoparbitr.venvLibsite-packagessqlalchemyormsession.py", line 2638, in add
util.raise_(
File "c:UsersWendeDesktoparbitr.venvLibsite-packagessqlalchemyutilcompat.py", line 211, in raise_
raise exception
sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.NoneType' is not mapped
bybit requires to release all resources with an explicit call to the .close() coroutine. If you are using the exchange instance with async coroutines, add `await exchange.close()` to your code into a place when you're done with the exchange and don't need the exchange instance anymore (at the end of your async coroutine).
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001D4EB190F90>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x000001D4EAA66740>, 1409138.843), (<aiohttp.client_proto.ResponseHandler object at 0x000001D4EAA66890>, 1409141.328)]']
connector: <aiohttp.connector.TCPConnector object at 0x000001D4EB190D50>

如果for k1, v1 in data.items():循环成功完成并且解释器在没有显式返回(这是Python的默认行为)的情况下离开函数,则函数response_to_sql将返回None

如果这种情况永远不可能发生,则调查数据和表包含什么。

无论如何,的函数必须返回SQLAlchemy映射对象可以返回None,这是一个错误。
async def response_to_sql(table):
for k1, v1 in data.items():
for t in table:
if table.__tablename__ == k1:     
for k2, v2 in v1.items():
ins = t(symbol=v2['symbol'], ask=v2['ask'], bid=v2['bid'])
return ins

相关内容

  • 没有找到相关文章

最新更新