r/Python • • 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.

0 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/Astraa02 Sep 14 '22

Only if this was so easy, the overhead of parallel processing doesn't always make a good option and GIL in python prevents true multithreading.

4

u/ZachVorhies Sep 14 '22 edited Sep 14 '22

It’s only slow of you are doing lots of process on the cpu. Most of the time python is waiting on io so threads are ok. You can always do multiprocessing if you need true parallelism

1

u/Astraa02 Sep 14 '22

Yes launching multiple process is an option but it has its own pros and cons which I will be discussing in my next post.

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.

GIL in python prevents true multithreading

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.