Hello I fairly new to Python and I am trying to convert an existing application I have on Flask into Quart (https://gitlab.com/pgjones/quart) which is supposed to be built on top of asyncio, so I can use Goblin OGM to interact with JanusGraph or TinkerPop. According to the examples I found on Goblin I need to obtain an event loop to run the commands asynchronously.
>>> import asyncio
>>> from goblin import Goblin
>>> loop = asyncio.get_event_loop()
>>> app = loop.run_until_complete(
... Goblin.open(loop))
>>> app.register(Person, Knows)
However I can't find a way to obtain the event loop from Quart even though it is build on top of asyncio.
Does anyone know how I can get that ? Any help will be highly appreciated.
TL;DR To obtain the event loop, call
asyncio.get_event_loop()
.In an asyncio-based application, the event loop is typically not owned by Quart or any other protocol/application level component, it is provided by asyncio or possibly an accelerator like uvloop. The event loop is obtained by calling
asyncio.get_event_loop()
, and sometimes set withasyncio.set_event_loop()
.This is what quart's
app.run()
uses to run the application, which means it works with the default event loop created by asyncio for the main thread. In your case you could simply call quart'srun()
after registeringGoblin
:The above should answer the question in the practical sense. But that approach wouldn't work if more than one component insisted on having their own
run()
method that spins the event loop - sinceapp.run()
doesn't return, you can only invoke one such function in a thread.If you look more closely, though, that is not really the case with
quart
either. While Quart examples do useapp.run()
to serve the application, if you take a look at the implementation ofapp.run()
, you will see that it calls the convenience functionrun_app()
, which trivially creates a server and spins up the main loop forever:If you need to control how the event loop is actually run, you can always do it yourself: