tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.68k stars 390 forks source link

Always encountering the error `'NoneType' object has no attribute 'write'`. #1453

Open tufbel opened 1 year ago

tufbel commented 1 year ago

I used apscheduler to start a scheduled task that runs every 5 seconds. However, the statement related to updating the database always throws an error after running for a while.

await Session.filter(id__in=[...])update(state=StateEnum.communication)

Traceback (most recent call last):

  File "D:\Dev\MyProject\WorkProjects\src\faster\routers\control\tasks.py", line 138, in __rcst_update_to_communication
    await asyncio.shield(
          │       └ <function shield at 0x000001F8C3F59EE0>
          └ <module 'asyncio' from 'D:\\Dev\\Miniconda3\\envs\\lib\\asyncio\\__init__.py'>
  File "D:\Dev\Miniconda3\envs\lib\asyncio\tasks.py", line 688, in _wrap_awaitable
    return (yield from awaitable.__await__())
                       │         └ <function UpdateQuery.__await__ at 0x000001F8C7CF8A60>
                       └ <tortoise.queryset.UpdateQuery object at 0x000001F8C93FF280>
  File "D:\Dev\Miniconda3\envs\lib\site-packages\tortoise\queryset.py", line 1109, in _execute
    return (await self._db.execute_query(str(self.query), self.values))[0]
                  │    │                     │    │       │    └ <member 'values' of 'UpdateQuery' objects>
                  │    │                     │    │       └ <tortoise.queryset.UpdateQuery object at 0x000001F8C93FF280>
                  │    │                     │    └ <member 'query' of 'AwaitableQuery' objects>
                  │    │                     └ <tortoise.queryset.UpdateQuery object at 0x000001F8C93FF280>
                  │    └ <member '_db' of 'AwaitableQuery' objects>
                  └ <tortoise.queryset.UpdateQuery object at 0x000001F8C93FF280>
  File "D:\Dev\Miniconda3\env\lib\site-packages\tortoise\backends\mysql\client.py", line 44, in translate_exceptions_
    return await func(self, *args)
                 │    │      └ ("UPDATE `control_session` SET `state`=%s WHERE `id` IN ('00-0a-35-00-01-59')", ['COMMUNICATION'])
                 │    └ <tortoise.backends.mysql.client.TransactionWrapper object at 0x000001F8C8C06580>
                 └ <function MySQLClient.execute_query at 0x000001F8C915C8B0>
  File "D:\Dev\Miniconda3\envs\lib\site-packages\tortoise\backends\mysql\client.py", line 199, in execute_query
    await cursor.execute(query, values)
          │      │       │      └ ['COMMUNICATION']
          │      │       └ "UPDATE `control_rcst_session` SET `state`=%s WHERE `rcst_hid` IN ('00-0a-35-00-01-59')"
          │      └ <method 'execute' of 'asyncmy.cursors.Cursor' objects>
          └ <asyncmy.cursors.Cursor object at 0x000001F8C9319AC0>
  File "asyncmy\cursors.pyx", line 179, in execute
    result = await self._query(query)
  File "asyncmy\cursors.pyx", line 364, in _query
    await conn.query(q)
  File "asyncmy\connection.pyx", line 449, in query
    await self._execute_command(COM_QUERY, sql)
                                └ 3
  File "asyncmy\connection.pyx", line 685, in _execute_command
    self._write_bytes(prelude + sql[: packet_size - 1])
  File "asyncmy\connection.pyx", line 624, in asyncmy.connection.Connection._write_bytes
    self._writer.write(data)
AttributeError: 'NoneType' object has no attribute 'write'