Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: This event loop is already running in python #97

Open
luznet opened this issue Aug 20, 2021 · 1 comment
Open

RuntimeError: This event loop is already running in python #97

luznet opened this issue Aug 20, 2021 · 1 comment

Comments

@luznet
Copy link

luznet commented Aug 20, 2021

Hello I received following error while running :
async def my_event_handler(e):
message=json.loads(e)
print(message)

matt.init_websocket(my_event_handler)

as a workaround I used following solution from stackoverflow:
https://stackoverflow.com/questions/46827007/runtimeerror-this-event-loop-is-already-running-in-python

@fried
Copy link
Contributor

fried commented Dec 14, 2021

init_websocket internally takes the default eventloop and expects to run it. This may not be what you or I expect. But it means the current event loop has to not be running when you call that method. This may result in some pretty weird looking bootstrap code on your part. I have a pull request where I try to provide an asyncio friendly interface #102

If you are using nested eventloops as the solution that fix is worse than the problem, It ends up making async python code more magical. By default the eventloop runs a single task at a time and which tasks is currently running can only switch at await keywords. So it is very easy to see where other tasks could be executing and updating some shared variables. With nested eventloop you can have task switches and executing any time you call a blocking function. Which means you need to use asyncio Locks even in code where it appears not to be possible for other tasks to be running due to the lack of an await keyword. This is completely unexpected in existing asyncio code and you will end up having problems with race conditions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants