Disadvantages of ThreadPoolExecutor?

590 views
Skip to first unread message

Tobias Günther

unread,
Feb 22, 2016, 10:20:11 AM2/22/16
to Tornado Web Server
I recently saw that it is possible to run synchronous functions asynchronously in tornado using a ThreadPoolExecutor.
(http://www.tornadoweb.org/en/stable/faq.html?highlight=threadpoolexecutor#why-isn-t-this-example-with-time-sleep-running-in-parallel)

My question is, if there are any disadvantages of doing this (besides the overhead of thread management)?

Or formulated the other way around: what advantages do I get from implementing an asynchronous client library for a webservice/db/etc. with tornados AsyncHTTPClient, instead of using an existing synchronous library and running each request to my tornado server in its own thread?

Thanks for your help!

INADA Naoki

unread,
Feb 22, 2016, 12:52:40 PM2/22/16
to python-...@googlegroups.com
There is great articule written by author of SQLAlchemy.

--
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.



--
INADA Naoki  <songof...@gmail.com>

Tobias Günther

unread,
Feb 22, 2016, 6:27:53 PM2/22/16
to Tornado Web Server
Thanks for the link, very interesting read!

So if it actually is a possibility that it might even be *advantageous* to use threads instead of coroutines, I will definitely try it out and compare the performance of the two options for my usecase.

If somebody was inclined to argue for single-threaded coroutines instead of using parallel threads, I would also be interested in hearing that opinion/reasoning.
I guess there was a reason to make coroutines the recommended way to write asynchronous code in Tornado.

Cheers!

A. Jesse Jiryu Davis

unread,
Feb 22, 2016, 6:47:28 PM2/22/16
to python-...@googlegroups.com
Mike Bayer's heuristic is this: if you have a small number of network operations, and / or those operations are guaranteed to complete quickly, threads may perform better.

If you have a very large number of operations, and / or those operations have unbounded duration, async (callbacks or coroutines) is better. The classic scenarios for slow operations are slow-polling or websockets, the kind of situation Tornado was invented for.

As Mike points out, when you talk to your database you are in the former scenario: you have control of your peer and you can guarantee it's fast. But whenever you talk across the Internet you're in the latter category.

As I wrote in response,


... it's especially important to use async callbacks or coroutines when talking to an untrusted peer across the Internet, since the peer may be deliberately slow in order to DOS you via "tarpit" or "slow loris". In that case, the attacker forces you to open many connections with very little activity on each, so async stands up better to the attack than threads.

Tobias Günther

unread,
Feb 23, 2016, 2:42:31 AM2/23/16
to Tornado Web Server
Thanks a lot for your answer, also watched your Pycon 2014 talk now, very interesting!
Reply all
Reply to author
Forward
0 new messages