asyncio event loop or IOLoop ?

809 views
Skip to first unread message

aliane abdelouahab

unread,
Jun 1, 2018, 6:05:14 PM6/1/18
to Tornado Web Server
Hi,
Since now Tornado plays perfectly with async/await, which means we can directly use Async/Await libraries for databases, right ?
for example this one for Posgresql https://magicstack.github.io/asyncpg/current/ (since momoko dont support Tornado 5.0),
So, will the Tornado's IOLoop plays perfectly with those libraries or do we have to add a bridge ?

Which one will take the global event loop ?

asyncio.get_event_loop().run_until_complete(some_function)

or

tornado.ioloop.IOLoop.instance().start()

Ben Darnell

unread,
Jun 2, 2018, 12:35:54 PM6/2/18
to python-...@googlegroups.com
On Fri, Jun 1, 2018 at 6:05 PM aliane abdelouahab <alabde...@gmail.com> wrote:
Hi,
Since now Tornado plays perfectly with async/await, which means we can directly use Async/Await libraries for databases, right ?
for example this one for Posgresql https://magicstack.github.io/asyncpg/current/ (since momoko dont support Tornado 5.0),
So, will the Tornado's IOLoop plays perfectly with those libraries or do we have to add a bridge ?

Yes, with Tornado 5.0 you can use asyncio-based libraries like asyncpg without doing anything special. 
 

Which one will take the global event loop ?

asyncio.get_event_loop().run_until_complete(some_function)

or

tornado.ioloop.IOLoop.instance().start()

You can do either one; they both start the asyncio event loop (run_forever() is the asyncio equivalent of tornado's start(). run_until_complete() is the equivalent of tornado's run_sync()). Python 3.7 introduces a function asyncio.run() which I'm personally going to start using instead of run_sync() because it's less to type. 

-Ben


 

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

aliane abdelouahab

unread,
Jun 2, 2018, 12:39:05 PM6/2/18
to Tornado Web Server
Ah, Thank you very much for this explaination,

I have another question, what is the best way to deal with databases connection in this case ? in the past the connection was done once, to avoid opening/closing which costs time, so what is the best way to initialize the connection with database and Tornado while using Async/Await ?

Ben Darnell

unread,
Jun 2, 2018, 12:44:07 PM6/2/18
to python-...@googlegroups.com
Using a single connection was a bit of a hack, because the fact that the database api was synchronous meant that there would be no sharing. With an asynchronous database driver, you should use a connection pool (for example, with asyncpg.create_pool), just like you would with a multi-threaded application. 

-Ben

aliane abdelouahab

unread,
Jun 2, 2018, 12:53:56 PM6/2/18
to Tornado Web Server
Got it, Thank you again 
Reply all
Reply to author
Forward
0 new messages