4

I am wondering, if python GIL allow only a single thread / process to run at once, why should I use asyncio, I get that switching between threads is expensive but, thats it? this is the only advantage of asyncio in python?

dsal3389
  • 658
  • 1
  • 7
  • 26
  • The whole point of asyncio is that you don't _need_ multiple threads if you just multiplex efficiently in one. The GIL doesn't stop you from multiplexing, it only stops you from threading; so the GIL makes asyncio _more_ necessary by making threaded performance worse. – Charles Duffy May 26 '22 at 18:13
  • Are you under the impression that the GIL automatically makes thread-based code thread-safe? It doesn't. The GIL protects the Python interpreter itself, not your code. – user2357112 May 26 '22 at 18:14
  • ...I'm not sure I understand why you would think the GIL makes async unnecessary. Questions should be clear and explicit; we shouldn't need to guess at your meaning. – Charles Duffy May 26 '22 at 18:15
  • @CharlesDuffy Taking about being clear and explicit...which "you" are you talking to? – Code-Apprentice May 26 '22 at 18:16
  • @Code-Apprentice, the OP, exclusively, when not explicitly addressing anyone else. – Charles Duffy May 26 '22 at 18:16
  • @CharlesDuffy Then you should `@` them since there are multiple contributors on this page. Also, where did the OP state that "the GIL makes async unnecessary"? – Code-Apprentice May 26 '22 at 18:17
  • @Code-Apprentice, that's my restatement of the best understanding I can make out of the question. If it's inaccurate, that's an opportunity for the OP to clarify what they _actually_ meant to say. In that context, understanding how someone fishing for meaning in their question might misinterpret it is hopefully helpful. – Charles Duffy May 26 '22 at 18:18
  • 2
    @Code-Apprentice, btw, Stack Overflow automatically removes `@`s directed at the OP of a question or comment, _because that's the assumed target_. See a discussion on [meta] on the topic, at https://meta.stackexchange.com/questions/97283/dont-remove-the-part-of-my-comment. (From that exchange you can gather that there's no small number of people who don't _like_ that behavior, but nonetheless, the site is designed with an assumption that `@`-ing the OP is unnecessary). – Charles Duffy May 26 '22 at 18:20

1 Answers1

7

Threading in Python is inefficient because of the GIL (Global Interpreter Lock) which means that multiple threads cannot be run in parallel as you would expect on a multi-processor system. Plus you have to rely on the interpreter to switch between threads, this adds to the inefficiency.

asyc/asyncio allows concurrency within a single thread. This gives you, as the developer, much more fine grained control of the task switching and can give much better performance for concurrent I/O bound tasks than Python threading.

The 3rd approach that you don't mention is multiprocessing. This approach uses processes for concurrency and allows programs to make full use of hardware with multiple cores.

Thornily
  • 533
  • 3
  • 15
  • 2
    I think a key thing to highlight is that `asyncio` is specifically meant to help when doing concurrent I/O. It allows you to have one thread send many (100s or 1000s) I/O requests at once, without ever blocking the thread. Normally you would need to have one thread per I/O request, which is costly in terms of memory usage and context switching (though it does not hold the GIL - Python releases the GIL during blocking I/O). If you're doing CPU-bound work that requires the GIL to be held, `asyncio` has no benefit at all, and `multiprocessing` is your best option to improve performance. – dano May 26 '22 at 19:58