19
Notes on Thread and threading module in python
This post encompasses some notes about thread and
Threading module in python, that I used as a quick recap.
Thread Is a basic unit of CPU Utlization, the smallest unit of processing that can be performed in an OS and an entity within a process. One process can have Multiple threads.
It contains specific information in a Thread Control Block (TCB) such as :
Thread ID which is assign to every new thread.
Stack pointer that contains the local variables under thread’s scope.
Program counter Or Register that contains the address of the current instruction being excuted by the Tthread.
Parent Process Pointer to point to Process control block
(PCB) of the parent process that this thread lives on.
Threading module provides a very simple and intuitive API for implementing multiple threads. Thread in this module encapsulates threads and provide an interface to workwith them.
Python has a complicated relationship with threading thanks to its GIL,
To create a new thread is by calling threading.Thread
from threading import Thread
def foo():
print("Hola Hola")
f1 = Thread(target = foo)
# the thread will never be excuted unless `start` is called
f1.start()
Note Start will run and terminated. Calling
thread_name.start
again will cuase aRuntimeError
19