Can someone explain to me that I drew python as if I were five?

I read the docs. I played with examples. But I still can't figure out what exactly asynchronously means when it is useful, and where does the magic of many people seem so crazy?

If this is just not to wait for I / O, why for a simple run in threads? Why do I need a Deferral?

I think that I lack the fundamental knowledge of computing, so these are the questions. If so, then what is it?

+4
source share
1 answer

how are you five ... ok: the poor are poor, the asink is good!

now, seriously: threads carry overhead - both when locking, and when switching the interpreter, as well as in memory consumption and code complexity. when your program is tied to IO and waits a lot for other services (APIs, databases) to return a response, you mostly expect downtime and waste resources.

The point of asynchronous I / O is to reduce the overhead of threads while maintaining concurrency, and keeping your program simple by avoiding deadlocks and reducing complexity.

think, for example, about a chat server. you have thousands of connections on the server and you want some people to receive messages depending on which room they are in. doing this with threads will be a lot harder than doing it in an asynchronous way.

re deferred is just a way to simplify the code, instead of giving each function a callback to return when an upcoming operation is ready.

another note - if you want a much simpler and sleeker structure of asynchronous I / O, try a tornado, basically it is an asynchronous web server, with an asynchronous http client and replacing pending ones. It is very beautifully written and can be used as a universal asynchronous I / O structure.

see http://tornadoweb.org

+4
source

Source: https://habr.com/ru/post/1400366/


All Articles