0 votes
in Python by

How is multithreading achieved in Python?

2 Answers

0 votes
by

Even though Python comes with a multi-threading package, if the motive behind multithreading is to speed the code then using the package is not the go-to option.

The package has something called the GIL or Global Interpreter Lock, which is a construct. It ensures that one and only one of the threads execute at any given time. A thread acquires the GIL and then do some work before passing it to the next thread.

This happens so fast that to a user it seems that threads are executing in parallel. Obviously, this is not the case as they are just taking turns while using the same CPU core. Moreover, GIL passing adds to the overall overhead to the execution.

Hence, if you intend to use the threading package for speeding up the execution, using the package is not recommended.

0 votes
by
Python has a multi-threading package ,but commonly not considered as good practice to use it as it will result in increased code execution time.

Python has a constructor called the Global Interpreter Lock (GIL). The GIL ensures that only one of your ‘threads’ can execute at one time.The process makes sure that a thread acquires the GIL, does a little work, then passes the GIL onto the next thread.

This happens at a very Quick instance of time and that’s why to the human eye it seems like your threads

Related questions

0 votes
asked Oct 11, 2021 in Python by rajeshsharma
0 votes
asked Sep 24, 2021 in Python by Robin
...