How is Multithreading achived in Python ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How is Multithreading achived in Python ?

A thread acquires GIL, does a little work, then passes the GIL onto the next thread.

29th Aug 2018, 12:14 PM
Shreyash Havale
Shreyash Havale - avatar
2 Answers
+ 4
import threading def function(): while True: print(”threading”) th = threading.Thread(target=function) th.start()
29th Aug 2018, 1:32 PM
Toni Isotalo
Toni Isotalo - avatar
+ 3
First import the threading module, define functions representing the tasks you want the threads to perform. Create thread instances and specify the target Use start() to start a thread, you can as well wait for a thread to finish using the join(). But you can save yourself the trouble by using other approaches like multiprocessing or asynchronous programming due to the fact that in Python because of the GIL, python threads are more suitable for I/O bound tasks than CPU-bound tasks.
6th Jan 2024, 9:11 PM
Izaiah Kay
Izaiah Kay - avatar