Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is python threading or multiprocessing at core of async calls?

I already worked with python async frameworks like Twisted and Tornado. Also I know that python already have native implementation of async calls via asyncio module. I thought that (threads, multiprocessing) and async calls are different concepts. But not long ago I watched a couple of videos related to threading and multiprocessing and seems that all this async staff build above them. Is it true?

like image 989
Viacheslav Kondratiuk Avatar asked Jan 07 '16 08:01

Viacheslav Kondratiuk


2 Answers

No, async calls is the way to structure a program. threading, multiprocessing may be used to implement some of these calls (but they are neither necessary nor common in Python asynchronous frameworks).

Concurrency is not parallelism:

In programming, concurrency is the composition of independently executing processes, while parallelism is the simultaneous execution of (possibly related) computations

Do not confuse how the program text is organized and how it is implemented (or executed). The exact same asynchronous code may be executed in a single thread, in multiple threads, in multiple processes. It is easy to switch between a simple Pool code that uses multiprocessing.Pool (processes), multiprocessing.dummy.Pool (threads), or their gevent-patched versions (single-threaded). Also, if there is only a single CPU then processes won't necessarily run in parallel but OS can make them run concurrently.

If by async you mean async keyword in Python then it means a generator function -- just one of the ways to create awaitable objects. asyncio is not the only way to consume such object e.g., there is curio which uses async functions but the backend is independent from asyncio. Recommended video: Python Concurrency From the Ground Up: LIVE!.

like image 142
jfs Avatar answered Sep 19 '22 11:09

jfs


No, generally, async is single-threaded, and to implement async absolutely does not require the use of multiple threads of processes (that's the whole point of async). But there are use cases where people may want to mix them together for whatever reason.

In this model [the async model], the tasks are interleaved with one another, but in a single thread of control. This is simpler than the threaded case because the programmer always knows that when one task is executing, another task is not. Although in a single-processor system a threaded program will also execute in an interleaved pattern, a programmer using threads should still think in terms of Figure 2, not Figure 3, lest the program work incorrectly when moved to a multi-processor system. But a single-threaded asynchronous system will always execute with interleaving, even on a multi-processor system.

Source: http://krondo.com/?p=1209

like image 21
absolutelyNoWarranty Avatar answered Sep 22 '22 11:09

absolutelyNoWarranty