112 views
0

因为我发现如果只在程序开头调用API。但有时需要重复得到数据时得到的不是更新数据。我就尝试把以下写入到循环中。数据是可以更新了。但会报错一大串信息后才成功。

api = TqApi(TqKq(), auth=”xxxx,pass”)
account = api.get_account()

以下是报错信息,每循环一次报错这一大串一次
RuntimeError: Event loop is closed
Exception ignored in: <coroutine object WebSocketCommonProtocol.close_connection at 0x000001C6C0D6D2C8>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py”, line 1206, in close_connection
if await self.wait_for_connection_lost():
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py”, line 1229, in wait_for_connection_lost
loop=self.loop if sys.version_info[:2] < (3, 8) else None,
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py”, line 426, in wait_for
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 656, in call_later
context=context)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 666, in call_at
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
2020-10-12 11:46:02,240 – INFO – 通知: 与 wss://otg-sim.shinnytech.com/trade 的网络连接已建立
2020-10-12 11:46:02,245 – INFO – 通知: 与 wss://free-api.shinnytech.com/t/nfmd/front/mobile 的网络连接已建立
2020-10-12 11:46:02,265 – INFO – 通知: 登录成功
SHFE开始运行2020-10-12 11:46:06
运行结束2020-10-12 11:46:06
DCE开始运行2020-10-12 11:46:06
运行结束2020-10-12 11:46:06
CZCE开始运行2020-10-12 11:46:06
运行结束2020-10-12 11:46:06
Task was destroyed but it is pending!
task: <Task pending coro=<TqApi._windows_patch() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py:2234> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6B826DAC8>()]> cb=[TqApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending coro=<TqApi._notify_watcher() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py:2242> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6BF441DF8>()]> cb=[TqApi._on_task_done()]>
Exception ignored in: <coroutine object TqApi._notify_watcher at 0x000001C6BC3E18C8>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py”, line 2242, in _notify_watcher
async for _ in update_chan:
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\channel.py”, line 110, in __anext__
value = await asyncio.Queue.get(self)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\queues.py”, line 161, in get
getter.cancel() # Just in case getter is not done yet.
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py”, line 1767, in _call_soon
return org_call_soon(callback, *args, **kargs)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 687, in call_soon
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending coro=<TqApi._connect() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py:2304> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6B826DC48>()]> cb=[TqApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending coro=<TqApi._md_handler() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py:1870> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6B826D1F8>()]> cb=[TqApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending coro=<TqApi._connect() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py:2304> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6C484E438>()]> cb=[TqApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending coro=<TqAccount._run() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\account.py:85> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at
0x000001C6B826DD38>()]> cb=[TqApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending coro=<TqWebHelper._run() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\tqwebhelper.py:79> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6B826D918>()]> cb=[TqApi._on_task_done()]>
Exception ignored in: <coroutine object TqApi._connect at 0x000001C6C0D35648>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py”, line 2404, in _connect
await send_task
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\client.py”, line 525, in __aexit__
await self.ws_client.close()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py”, line 679, in close
loop=self.loop if sys.version_info[:2] < (3, 8) else None,
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py”, line 426, in wait_for
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 656, in call_later
context=context)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 666, in call_at
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\_weakrefset.py:48: RuntimeWarning: coroutine ‘WebSocketCommonProtocol.write_close_frame’ was never awaited
self._iterating = set()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Task was destroyed but it is pending!
task: <Task pending coro=<TqApi._send_handler() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py:2441> wait_for=<Future cancelled>>
Exception ignored in: <coroutine object TqApi._md_handler at 0x000001C6BCC8E0C8>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py”, line 1870, in _md_handler
async for pack in origin_recv_chan:
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\channel.py”, line 110, in __anext__
value = await asyncio.Queue.get(self)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\queues.py”, line 161, in get
getter.cancel() # Just in case getter is not done yet.
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py”, line 1767, in _call_soon
return org_call_soon(callback, *args, **kargs)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 687, in call_soon
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
Exception ignored in: <coroutine object TqApi._connect at 0x000001C6BC1E49C8>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py”, line 2404, in _connect
await send_task
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\client.py”, line 525, in __aexit__
await self.ws_client.close()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py”, line 679, in close
loop=self.loop if sys.version_info[:2] < (3, 8) else None,
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py”, line 426, in wait_for
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 656, in call_later
context=context)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 666, in call_at
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending coro=<TqApi._send_handler() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py:2441> wait_for=<Future cancelled>>
Exception ignored in: <coroutine object TqAccount._run at 0x000001C6BC1E4048>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\account.py”, line 91, in _run
md_task.cancel()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py”, line 1767, in _call_soon
return org_call_soon(callback, *args, **kargs)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 687, in call_soon
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending coro=<TqAccount._md_handler() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\account.py:95> wait_for=<Future cancelled>>
Exception ignored in: <coroutine object TqWebHelper._run at 0x000001C6BC1E4EC8>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\tqwebhelper.py”, line 84, in _run
_data_handler_without_web_task.cancel()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py”, line 1767, in _call_soon
return org_call_soon(callback, *args, **kargs)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 687, in call_soon
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending coro=<TqWebHelper._data_handler_without_web() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\tqwebhelper.py:153> wait_for=<Future cancelled>>
Task was destroyed but it is pending!
task: <Task pending coro=<TqAccount._td_handler() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\account.py:102> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6C484E1F8>()]>>
Exception ignored in: <coroutine object TqAccount._td_handler at 0x000001C6B7EB0548>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\account.py”, line 102, in _td_handler
async for pack in td_recv_chan:
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\channel.py”, line 110, in __anext__
value = await asyncio.Queue.get(self)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\queues.py”, line 161, in get
getter.cancel() # Just in case getter is not done yet.
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\tqsdk\api.py”, line 1767, in _call_soon
return org_call_soon(callback, *args, **kargs)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 687, in call_soon
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketCommonProtocol.transfer_data() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py:827> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6BB72D498>()]> cb=[<TaskWakeupMethWrapper object at 0x000001C6C1BDC4F8>()]>
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketCommonProtocol.keepalive_ping() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py:1121> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6C93B86A8>()]>>
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketCommonProtocol.close_connection() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py:1169> wait_for=<Task pending coro=<WebSocketCommonProtocol.transfer_data() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py:827> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6BB72D498>()]> cb=[<TaskWakeupMethWrapper object at 0x000001C6C1BDC4F8>()]>>
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketCommonProtocol.transfer_data() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py:827> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6B826DCD8>()]> cb=[<TaskWakeupMethWrapper object at 0x000001C6C1BDC708>()]>
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketCommonProtocol.keepalive_ping() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py:1121> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6C93B8FA8>()]>>
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketCommonProtocol.close_connection() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py:1169> wait_for=<Task pending coro=<WebSocketCommonProtocol.transfer_data() running at C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py:827> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000001C6B826DCD8>()]> cb=[<TaskWakeupMethWrapper object at 0x000001C6C1BDC708>()]>>
Exception ignored in: <coroutine object WebSocketCommonProtocol.close_connection at 0x000001C6C0D6D3C8>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py”, line 1206, in close_connection
if await self.wait_for_connection_lost():
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py”, line 1229, in wait_for_connection_lost
loop=self.loop if sys.version_info[:2] < (3, 8) else None,
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py”, line 426, in wait_for
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 656, in call_later
context=context)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 666, in call_at
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)
RuntimeError: Event loop is closed
Exception ignored in: <coroutine object WebSocketCommonProtocol.close_connection at 0x000001C6BA0349C8>
Traceback (most recent call last):
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py”, line 1206, in close_connection
if await self.wait_for_connection_lost():
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\site-packages\websockets\protocol.py”, line 1229, in wait_for_connection_lost
loop=self.loop if sys.version_info[:2] < (3, 8) else None,
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py”, line 426, in wait_for
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 656, in call_later
context=context)
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 666, in call_at
self._check_closed()
File “C:\Users\forrestmonkey\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py”, line 479, in _check_closed
raise RuntimeError(‘Event loop is closed’)

Answered question
0

可以循环这个调用

account = api.get_account() #这个可以放在循环里

但不要循环创建Api

api = TqApi(TqKq(), auth=”xxxx,pass”) #这个不能放在循环里

Answered question
You are viewing 1 out of 1 answers, click here to view all answers.