Logo

dev-resources.site

for different kinds of informations.

Notes on Thread and threading module in python

Published at
10/5/2021
Categories
python
thread
Author
rowida46
Categories
2 categories in total
python
open
thread
open
Author
8 person written this
rowida46
open
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.

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()
Enter fullscreen mode Exit fullscreen mode

Note Start will run and terminated. Calling thread_name.start again will cause a RuntimeError

You can find the code in the following repository, It's a chapter within a repository for the book Python for professional Book repo.

To Read:

thread Article's
30 articles in total
Favicon
This Small Python Script Improved Understanding of Low-Level Programming
Favicon
How to Run an Asynchronous Task in Spring WebFlux Without Blocking the Main Response?
Favicon
Quem comeu o meu CompletableFuture?
Favicon
Concurrency and Parallelism in PHP
Favicon
Thread fundamentals in Java
Favicon
Node Boost: Clusters & Threads
Favicon
Executando processos paralelos com Flutter/DART
Favicon
Multithreading - Dining Philosophers Problem in Java
Favicon
Multi-Threaded Programs in Python Using threading Module
Favicon
newSingleThreadContext() causes outofmemoryexeception in my service on Android
Favicon
A Battle of Words: Twitter vs Threads App
Favicon
Make Ruby code thread-safe
Favicon
Process and Thread
Favicon
Asynchronous Daily Thread Automation
Favicon
How Home Assistant found my Thread Thermostats
Favicon
Web Worker, Service Worker, and Worklets: A Comprehensive Guide
Favicon
O que รฉ processo e um thread?
Favicon
The experiment of SPVM::Thread is started today.
Favicon
Multi-Threaded FizzBuzz
Favicon
Tweet YouTube video with Google Chrome Extension
Favicon
Internals of goroutines and Channels
Favicon
Thread Synchronization within Linux Operating System.
Favicon
Java Thread Sฤฑnฤฑfฤฑ
Favicon
Why do we need threads along with processes?
Favicon
Java Thread Programming (Part 1)
Favicon
Notes on Thread and threading module in python
Favicon
Java Thread Programming: Lesson 3
Favicon
Java Thread Programming: Lesson 1
Favicon
Java Thread Programming: Lesson 2
Favicon
How to create a new Thread in java ?

Featured ones: