Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

RuntimeError: cannot enter context: <_contextvars.Context object at %> is already entered #473

Closed
PerettoLucas opened this issue May 4, 2022 · 1 comment

Comments

@PerettoLucas
Copy link

Hi there,

I know there are probably plenty of issues addressing this problem, but still i didn't figure out how to fix this.

tying to work with nested event-loops more in de code example :
image

This code snippet throws the following error:

^CTraceback (most recent call last):
File "/usr/local/lib/python3.10/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
RuntimeError: cannot enter context: <_contextvars.Context object at %> is already entered

Obviously if i take out the nest_asyncio.apply() i get this error : RuntimeError: This event loop is already running
*also tried it with the util.patchAsyncio()

OS : linux
python version : 3.10
ib-insync version : 0.9.70

So how am i supposed to handle this ?

Now the fun part :
Take the same code and run it on :
OS : Windows 10
python version : 3.10
ib-insync version : 0.9.70

and magic starts happening.... it works ?!

would be thankful if someone could help.

best regards Lucas

@erdewit
Copy link
Owner

erdewit commented May 7, 2022

The issue is a blocking call (whatIfOrder) inside the onData event handler. This is easily remedied by using whatIfOrderAsync instead and turning the event handler into a coroutine (async def onData). There is no nesting needed.

The code should fail on Windows as well. Perhaps it was tried with placeOrder instead, which happens to be a non-blocking method and works as-is.

@erdewit erdewit closed this as completed May 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants