Open habibutsu opened 6 years ago
This can be produced even more simple way with using init_connection
instead init_db
async def init_connection():
app.reader, app.writer = await asyncio.open_connection(
'127.0.0.1', 9000, loop=app.loop)
In asyncio it's catastrophic to try to share event loop between processes.
Japronto is a forking server, as in, to server http requests it would spawn a pool of processes each with it's own asyncio loop.
By running app.loop.run_until_complete
in the parent process you initialize loop in the parent process and at the time you run app.run(debug=True)
you gonna spawn a child process (to server requests) which would share the loop with the parent and lead to an error you are seeing.
Probably I could detect this common mistake and give a better error message.
Also this is a big limitation in design. There should be a post_fork
call for each of the workers to open your connections, do per worker initialization. You could hacck one by looking in the code of run
.
Also depending on what you wanna do temporarily to overcome this you could connect
to the database when the first request comes in (in the child process).
Some of the related issues
The following script cause of crashing of server:
Output:
Note after deleting string with initialization of database everything works fine