0

每次我尝试连接到 Ibt 时,都会收到运行时错误“此事件循环已在运行”。我不记得我之前有过这个错误,可能是我开始了一个没有结束的循环。这是代码和错误日志:

ib=IB()
ib.connect('127.0.0.1',7497,12,10)


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-274dfdfc7ea3> in <module>
      1 ib=IB()
----> 2 ib.connect('127.0.0.1',7497,12,10)
      3 
      4 

~\Anaconda3\lib\site-packages\ib_insync\ib.py in connect(self, host, port, clientId, timeout, readonly)
    255         """
    256         return self._run(self.connectAsync(
--> 257             host, port, clientId, timeout, readonly))
    258 
    259     def disconnect(self):

~\Anaconda3\lib\site-packages\ib_insync\ib.py in _run(self, *awaitables)
    288 
    289     def _run(self, *awaitables: List[Awaitable]):
--> 290         return util.run(*awaitables, timeout=self.RequestTimeout)
    291 
    292     def waitOnUpdate(self, timeout: float = 0) -> bool:

~\Anaconda3\lib\site-packages\ib_insync\util.py in run(timeout, *awaitables)
    249         globalErrorEvent.connect(onError)
    250         try:
--> 251             result = loop.run_until_complete(task)
    252         except asyncio.CancelledError as e:
    253             raise globalErrorEvent.value() or e

~\Anaconda3\lib\asyncio\base_events.py in run_until_complete(self, future)
    564         future.add_done_callback(_run_until_complete_cb)
    565         try:
--> 566             self.run_forever()
    567         except:
    568             if new_task and future.done() and not future.cancelled():

~\Anaconda3\lib\asyncio\base_events.py in run_forever(self)
    519         self._check_closed()
    520         if self.is_running():
--> 521             raise RuntimeError('This event loop is already running')
    522         if events._get_running_loop() is not None:
    523             raise RuntimeError(

RuntimeError: This event loop is already running

任何想法我该如何解决这个问题?

4

1 回答 1

0

你应该写:

from ib_insync import *

util.startLoop()
ib=IB()
ib.connect('127.0.0.1', 7497, clientId=12)
于 2019-10-18T09:57:27.963 回答