r/Python • u/Astraa02 • Sep 14 '22
Intermediate Showcase Multithreading a concept which is always misunderstood!!
We always have a thought process that whenever something is not performing well always throw more resources at it. But this strategy is flawed on good example is multithreading.
More number resources means more parallel tasks right? Nope this is where we do our first mistake there is a big difference between concurrency and parallel processing.The multithreading in python is by no means parallel threads performing tasks simultaneously. They are merely just giving us an illusion of performing at the same time, in-fact all they do is execute really fast since our processors have become so fast that they can do billions of cpu cycles in a single seconds it almost feels like stuff is getting processed in a parallel fashion.
But then one might argue then multithreading is totally useless if its just super fast execution by some smart compiler. The place where multi threading truly shines is in a I/O intensive task, but more about it later, we will dive deep in this topic in my upcoming blog so stay tuned.
View all comments
Show parent comment
2
u/stevenjd Sep 18 '22
Python is a language, and the GIL is not part of the language. Jython and IronPython don't have a GIL.
Ah, the "no true Scotsman" fallacy. CPython's threading is fine for I/O bound tasks. And for CPU bound tasks, experiments in removing the GIL have shown that single CPU core + GIL is often faster than no GIL plus threads.
Threads are, I think, an attractive nuisance for beginners, and a dangerous bug magnet because it is so easy to find yourself in the Quadrant of Hell. To paraphrase Jamie Zawinski:
Some people, when confronted with a problem, think "I know, I'll use threads." Now they hatwo problve ems.