R
R
Rouslan9432020-12-01 07:14:16
Multithreading
Rouslan943, 2020-12-01 07:14:16

Multithreading vs asynchrony?

Good day ... I would like to know the difference between these approaches. Doesn't asynchronous programming imply something like multithreading? When do tasks run on the same thread?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pazukdev, 2021-01-20
@pazukdev

Multithreading vs asynchrony
Restaurant. The client ordered scrambled eggs and toast. 
1. Synchronous execution: we give the task to 1 cook. He makes the eggs first. After she is ready, he proceeds to the croutons.
2. Asynchronous execution (multithreading): we give the task to 2 cooks. They start conditionally at the same time. One makes scrambled eggs, the other makes toast, and both are in conflict over access to shared resources (pepper, salt, frying pan).
3. Asynchronous execution (1 thread): we give the task to 1 cook. He puts the eggs on the stove and sets the timer. Puts croutons and also sets a timer. While the timers have not worked, in order not to waste time, he tidies up the kitchen. After both timers work, it gives the order.

D
Dr. Bacon, 2020-12-01
@bacon

If without nuances:
Multithreading - switching between tasks is performed by python, you need to understand what GIL and release GIL are.
Asynchrony - one thread - event loop, the task itself signals that it is "completed" (well, or with a stretch, the programmer "controls" the switch), the main problem is that the task must be non-blocking, otherwise it all comes down to executing synchronous code in one thread.

R
rPman, 2020-12-01
@rPman

Differences in the approaches of the corresponding methods, whose work must be performed for a long time and wait for something (for example, network interaction, but not necessarily by itself).
Synchronous execution - this means all your methods that perform some task work until this task is completed, for example, sending data to the network, writing to disk (with reservations, there is a file writing mode in which data is written to a file in a buffer in memory, and the actual recording happens in parallel afterwards). In this method, it is impossible to run two simultaneous methods, for example, writing to disk and reading from the network - which is very inefficient in terms of resource consumption, since while the disk is being written to, the network is not loaded and vice versa.
Asynchronous execution - this means that the call to the start of the action is completed at the same moment as it was called, starting the download process in some way (often on a different thread, but not necessarily) and gives you some tools to get information about whether the work has completed or not, for example, callback methods that are called upon completion of work or an error, in the parameters of which the result will be returned (or there will be other methods to obtain it). Those. you can run multiple asynchronous actions at the same time, using resources efficiently.
Multi-threaded - in the context of the above described task, this is the approach of simultaneously launching several synchronous methods that are ready for such a launch (this clause requires notes of a kilometer per two texts) in separate threads (threads and / or processes) - special operating system entities that work independently and simultaneously (more precisely, pseudo-simultaneously, for example, you have 16 processor cores and you have 100 simultaneously working threads, so that they work simultaneously, the system itself periodically transfers active threads between the cores for a short time - milliseconds, freezing the rest, creating the illusion of simultaneous operation), thus your methods work simultaneously, but you have to spend energy on synchronization.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question