2

I am pretty new to Tornado. I can't understand the difference between using run_on_executor and defining an async method. Is it the same? Is the one multithreaded and the other not?

Thank you in advance.

Lore
  • 1,286
  • 1
  • 22
  • 57

2 Answers2

5

run_on_executor is for interfacing with blocking non-async code.

You are correct that async code is only executed in a single thread. Maybe an example would illustrate the point.

Let's say your Tornado web service interfaces with a library that makes use of requests to fetch country info for a given IP address. Since requests is a non-async library, calling this function would block the Tornado event loop.

So, you have two options: try to find the replacement for the library that is async-compatible OR run the blocking code in a different thread/process and have your event loop await its result like for normal async code without blocking the event loop. The latter option is run_on_executor which allows you to run the task in different thread or process, and asyncio would "await" its completion.

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
3

From the docs

Utilities for working with Future objects.

Tornado previously provided its own Future class, but now uses asyncio.Future. This module contains utility functions for working with asyncio.Future in a way that is backwards-compatible with Tornado’s old Future implementation.

While this module is an important part of Tornado’s internal implementation, applications rarely need to interact with it directly.

As I understand run_on_executor from tornado.concurrent is a way of interacting with asyncio.Future with backwards compatibility to old Tornado's Future

In any case, any code based in asyncio is not using multithreading, it's using coroutines

This medium post may help you understand the differences between asyncio and threads: here

David Bros
  • 94
  • 6
  • 1
    In any case, any code based in asyncio is not using multithreading, it's using coroutines Are coroutines and multithreading essentially the same thing? – Lore Jul 15 '20 at 09:27
  • 2
    The docs explain it better than I ever will, for clarification on coroutines (they are a little bit difficult to grasp due to the name) -> https://stackoverflow.com/questions/1934715/difference-between-a-coroutine-and-a-thread A coroutine is basically an event loop running on a single thread (look at Concurrency and Multithreading). -> https://docs.python.org/3/library/asyncio-dev.html – David Bros Jul 15 '20 at 09:42
  • 3
    the main difference between threads and coroutines is how they are scheduled. threads are pre-emptive This means that threads do not need to explictly yield control to their scheduler, the scheduler will preempt threads itself. Where as in case coroutines each coroutine is resposinble for yielding their control, Otherwise they will block all of the other coroutines. – Uku Loskit Jul 15 '20 at 09:58