382 浏览
0

# !/user/bin/env python3
# -*- coding: utf-8 -*-
  # 引入模块
from tqsdk import TqApi, TqAuth, TqSim
import asyncio
 async def main():
 # Create TqApi and TqSim objects for testing
 acc = TqSim ( )
 api = await asyncio.create_task (TqApi (acc, auth=TqAuth ("132xxxxxxxx", "qbjxxxxxxxx")))
 print(f"type(api):{type(api)}")
 api.close ( )
  # 对这个函数进行测试
if __name__ == '__main__':
 # Create TqApi and TqSim objects for testing
 asyncio.run(main())

测试结果:

C:\ProgramData\anaconda3\envs\lh166\python.exe -X pycache_prefix=C:\Users\qbjst\AppData\Local\JetBrains\PyCharm2023.3\cpython-cache “C:/Program Files/JetBrains/PyCharm 2023.3.4/plugins/python/helpers/pydev/pydevd.py” –multiprocess –qt-support=auto –client 127.0.0.1 –port 64488 –file H:\code\src\lh15\开始引入API\api引用2.py
Connected to pydev debugger (build 233.14475.56)
在使用天勤量化之前,默认您已经知晓并同意以下免责条款,如果不同意请立即停止使用:https://www.shinnytech.com/blog/disclaimer/
Traceback (most recent call last):
File “C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\api.py”, line 297, in __init__
if not self.wait_update(deadline=deadline): # 等待连接成功并收取截面数据
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\api.py”, line 1878, in wait_update
raise Exception(
Exception: TqSdk 使用了 python3 的原生协程和异步通讯库 asyncio,您所使用的 IDE 不支持 asyncio, 请使用 pycharm 或其它支持 asyncio 的 IDE

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Program Files\JetBrains\PyCharm 2023.3.4\plugins\python\helpers\pydev\pydevd.py”, line 1534, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Program Files\JetBrains\PyCharm 2023.3.4\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+”\n”, file, ‘exec’), glob, loc)
File “H:\code\src\lh15\开始引入API\api引用2.py”, line 20, in <module>
asyncio.run(main())
File “C:\Program Files\JetBrains\PyCharm 2023.3.4\plugins\python\helpers-pro\pydevd_asyncio\pydevd_nest_asyncio.py”, line 120, in run
return loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Program Files\JetBrains\PyCharm 2023.3.4\plugins\python\helpers-pro\pydevd_asyncio\pydevd_nest_asyncio.py”, line 211, in run_until_complete
return f.result()
^^^^^^^^^^
File “C:\ProgramData\anaconda3\envs\lh166\Lib\asyncio\futures.py”, line 203, in result
raise self._exception.with_traceback(self._exception_tb)
File “C:\ProgramData\anaconda3\envs\lh166\Lib\asyncio\tasks.py”, line 314, in __step_run_and_handle_result
result = coro.send(None)
^^^^^^^^^^^^^^^
File “H:\code\src\lh15\开始引入API\api引用2.py”, line 12, in main
api = await asyncio.create_task (TqApi (acc, auth=TqAuth (“13xxxxxxxx”, “qbjxxxxxxxx”)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\api.py”, line 301, in __init__
self.close()
File “C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\api.py”, line 352, in close
raise Exception(
Exception: TqSdk 使用了 python3 的原生协程和异步通讯库 asyncio,您所使用的 IDE 不支持 asyncio, 请使用 pycharm 或其它支持 asyncio 的 IDE
Task was destroyed but it is pending!
task: <Task pending name=’Task-8′ coro=<BaseSim._run() running at C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\tradeable\sim\basesim.py:61> cb=[TqBaseApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending name=’Task-4′ coro=<TqConnect._run() running at C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\connect.py:110> cb=[TqBaseApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending name=’Task-5′ coro=<TqReconnect._run() running at C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\connect.py:270> cb=[TqBaseApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending name=’Task-6′ coro=<TqSymbols._run() running at C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\symbols.py:15> cb=[TqBaseApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending name=’Task-10′ coro=<DataExtension._run() running at C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\data_extension.py:74> cb=[TqBaseApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending name=’Task-7′ coro=<TqTradingStatus._run() running at C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\trading_status.py:16> cb=[TqBaseApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending name=’Task-3′ coro=<TqApi._notify_watcher() running at C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\api.py:3634> cb=[TqBaseApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending name=’Task-9′ coro=<TqWebHelper._run() running at C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\tqwebhelper.py:74> cb=[TqBaseApi._on_task_done()]>
Task was destroyed but it is pending!
task: <Task pending name=’Task-2′ coro=<TqBaseApi._windows_patch() running at C:\Users\qbjst\AppData\Roaming\Python\Python312\site-packages\tqsdk\baseApi.py:116> cb=[TqBaseApi._on_task_done()]>
sys:1: RuntimeWarning: coroutine ‘BaseSim._run’ was never awaited
sys:1: RuntimeWarning: coroutine ‘TqConnect._run’ was never awaited
sys:1: RuntimeWarning: coroutine ‘TqReconnect._run’ was never awaited
sys:1: RuntimeWarning: coroutine ‘TqSymbols._run’ was never awaited
sys:1: RuntimeWarning: coroutine ‘DataExtension._run’ was never awaited
sys:1: RuntimeWarning: coroutine ‘TqTradingStatus._run’ was never awaited
sys:1: RuntimeWarning: coroutine ‘TqApi._notify_watcher’ was never awaited
sys:1: RuntimeWarning: coroutine ‘TqWebHelper._run’ was never awaited
sys:1: RuntimeWarning: coroutine ‘TqBaseApi._windows_patch’ was never awaited

Process finished with exit code 1

应该不是pycharm的原因:

#!/user/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
 async def my_coroutine():
    # Some code here
    await asyncio.sleep(1)
    print("你好")
    # More code here
 async def main():
    await my_coroutine()
 asyncio.run(main())

结果:

C:\ProgramData\anaconda3\envs\lh166\python.exe -X pycache_prefix=C:\Users\qbjst\AppData\Local\JetBrains\PyCharm2023.3\cpython-cache “C:/Program Files/JetBrains/PyCharm 2023.3.4/plugins/python/helpers/pydev/pydevd.py” –multiprocess –qt-support=auto –client 127.0.0.1 –port 64633 –file H:\code\src\lh15\开始引入API\协程测试4.py
Connected to pydev debugger (build 233.14475.56)
你好

Process finished with exit code 0

pycharm的协程功能一切正常

李思恒 已回答的问题 2024年3月14日
0

这个异常一般来说是,在其他的一般异常都没触发的情况下,异步编程触发的通用报错。

李思恒 已回答的问题 2024年3月14日
您正在查看1个答案中的1个,单击此处查看所有答案。