erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.75k stars 723 forks source link

Assertation error on connection #92

Closed tfrojd closed 5 years ago

tfrojd commented 5 years ago

I run ib_insync on an anaconda installation (python 3.6) on Debian.

from ib_insync import *
ib = IB()
ib.connect('ip_here', portno, clientId=12)

I then get the following error:

--------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-10-88bbb3c3d188> in <module>()
      1 ib = IB()
----> 2 ib.connect('35.205.54.121', 4001, clientId=12)

~/anaconda3/lib/python3.6/site-packages/ib_insync/ib.py in connect(self, host, port, clientId, timeout)
    200         This method is blocking.
    201         """
--> 202         self._run(self.connectAsync(host, port, clientId, timeout))
    203         return self
    204 

~/anaconda3/lib/python3.6/site-packages/ib_insync/ib.py in _run(self, *awaitables)
    234 
    235     def _run(self, *awaitables):
--> 236         return util.run(*awaitables, timeout=self.RequestTimeout)
    237 
    238     def waitOnUpdate(self, timeout: float=0) -> True:

~/anaconda3/lib/python3.6/site-packages/ib_insync/util.py in run(timeout, *awaitables)
    245         if timeout:
    246             future = asyncio.wait_for(future, timeout)
--> 247         result = syncAwait(future)
    248     return result
    249 

~/anaconda3/lib/python3.6/site-packages/ib_insync/util.py in syncAwait(future)
    362         result = _syncAwaitQt(future)
    363     else:
--> 364         result = _syncAwaitAsyncio(future)
    365     return result
    366 

~/anaconda3/lib/python3.6/site-packages/ib_insync/util.py in _syncAwaitAsyncio(future)
    368 def _syncAwaitAsyncio(future):
    369     assert asyncio.Task is asyncio.tasks._PyTask, \
--> 370             'To allow nested event loops, use util.patchAsyncio()'
    371     loop = asyncio.get_event_loop()
    372     task = asyncio.tasks.ensure_future(future)

AssertionError: To allow nested event loops, use util.patchAsyncio()

Any ideas why this happens?

tfrojd commented 5 years ago

Figured it is because I the example is not made to run in a notebook but only from a script.

It is clear from the example notebook but maybe a more informative error messsage or mentioning this with the README.md example would save other beginners some time.

erdewit commented 5 years ago

I've updated the readme code example to include util.startLoop(), like in the notebook examples.