Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Multithreading
  5. Threading module

Threading module

A thread is a sequence of instructions in a program which can be executed independently of other code. We can also say that thread is subset of a process and a process is made up of many threads.

Threads sometimes called light-weight processes, and they do not require much memory overhead; they are cheaper than processes.

A thread has a starting point, an execution sequence, and a result.

We have a module thread in python. We can easily import it in our program.

syntax:-

import thread

We have another module threading.

Threading module is used for creating, controlling and managing threads in python program.

Syntax:-

import threading

Some common functions of the threading module in python:-

  • threading.activeCount() − Returns the number of thread objects that are active.
  • threading.currentThread() − Returns the number of thread objects in the caller’s thread control.
  • threading.enumerate() − Returns a list of all thread objects that are currently active.

 The threading module also provides many classes whose objects are very useful in creating and managing threads.

  • thread- Object that represents a single thread of execution.
  • Lock- Primitive lock object.
  • Timer- Similar to Thread, except that it waits for a specified period of time before running.

This is all about threading module in python.

Was this article helpful to you? Yes No

How can we help?