This post encompasses some notes about thread and
Threading module in python, that I used as a quick recap.
What is Thread.
Thread Is a basic unit of CPU Utilization, the smallest unit of processing that can be performed in OS and an entity within a process. One process can have Multiple threads.
What thread contains?
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 executed by the Thread.
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 work with 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 executed unless `start` is called
f1.start()
Note Start will run and terminated. Calling
thread_name.start
again will cause aRuntimeError
You can find the code in the following repository, It's a chapter within a repository for the book Python for professional Book repo.
Top comments (0)