[Go to site: main page, start]

0% found this document useful (0 votes)
52 views9 pages

Python Multithreading Basics

This document provides an overview of multithreaded programming in Python. It discusses how to create and manage multiple threads to run tasks concurrently. The threading and thread modules in Python provide APIs for spawning new threads and synchronizing thread execution. Custom threads can be created by subclassing the Thread class and overriding run(). Methods like start(), join(), acquire() and release() enable starting, waiting for, and synchronizing threads.

Uploaded by

patricia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views9 pages

Python Multithreading Basics

This document provides an overview of multithreaded programming in Python. It discusses how to create and manage multiple threads to run tasks concurrently. The threading and thread modules in Python provide APIs for spawning new threads and synchronizing thread execution. Custom threads can be created by subclassing the Thread class and overriding run(). Methods like start(), join(), acquire() and release() enable starting, waiting for, and synchronizing threads.

Uploaded by

patricia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Introduction to Multithreading
  • Threading Module
  • Synchronizing Threads
  • Multithreaded Priority Queue
  • Processing Data with Threads

9/7/22, 5:07 PM Python - Multithreaded Programming

Python - Multithreaded Programming

Python Programming

26 Lectures 7.5 hours

 DATAhill Solutions Srinivas Reddy

More Detail

Python For Beginners: Learn Python Programming (Python 3)

60 Lectures 2 hours

 Jason Cannon

More Detail

Practical Python: Learn Python Basics Step By Step - Python 3

54 Lectures 3.5 hours

[Link] 1/9
9/7/22, 5:07 PM Python - Multithreaded Programming

 Edouard Renard
More Detail

Running several threads is similar to running several different programs concurrently, but with the
following benefits −

Multiple threads within a process share the same data space with the main thread and can
therefore share information or communicate with each other more easily than if they were
separate processes.

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

A thread has a beginning, an execution sequence, and a conclusion. It has an instruction pointer
that keeps track of where within its context it is currently running.

It can be pre-empted (interrupted)

It can temporarily be put on hold (also known as sleeping) while other threads are running -
this is called yielding.

Starting a New Thread


To spawn another thread, you need to call following method available in thread module −

thread.start_new_thread ( function, args[, kwargs] )

This method call enables a fast and efficient way to create new threads in both Linux and
Windows.

The method call returns immediately and the child thread starts and calls function with the
passed list of args. When function returns, the thread terminates.

Here, args is a tuple of arguments; use an empty tuple to call function without passing any
arguments. kwargs is an optional dictionary of keyword arguments.

Example
#!/usr/bin/python

import thread

import time

# Define a function for the thread

def print_time( threadName, delay):

count = 0

while count < 5:

[Link] 2/9
9/7/22, 5:07 PM Python - Multithreaded Programming

[Link](delay)

count += 1

print "%s: %s" % ( threadName, [Link]([Link]()) )

# Create two threads as follows

try:

thread.start_new_thread( print_time, ("Thread-1", 2, ) )

thread.start_new_thread( print_time, ("Thread-2", 4, ) )

except:

print "Error: unable to start thread"

while 1:

pass

When the above code is executed, it produces the following result −

Thread-1: Thu Jan 22 15:42:17 2009

Thread-1: Thu Jan 22 15:42:19 2009

Thread-2: Thu Jan 22 15:42:19 2009

Thread-1: Thu Jan 22 15:42:21 2009

Thread-2: Thu Jan 22 15:42:23 2009

Thread-1: Thu Jan 22 15:42:23 2009

Thread-1: Thu Jan 22 15:42:25 2009

Thread-2: Thu Jan 22 15:42:27 2009

Thread-2: Thu Jan 22 15:42:31 2009

Thread-2: Thu Jan 22 15:42:35 2009

Although it is very effective for low-level threading, but the thread module is very limited
compared to the newer threading module.

The Threading Module


The newer threading module included with Python 2.4 provides much more powerful, high-level
support for threads than the thread module discussed in the previous section.

The threading module exposes all the methods of the thread module and provides some
additional methods −

[Link]() − Returns the number of thread objects that are active.

[Link]() − Returns the number of thread objects in the caller's thread


control.

[Link]() − Returns a list of all thread objects that are currently active.

In addition to the methods, the threading module has the Thread class that implements
threading. The methods provided by the Thread class are as follows −

[Link] 3/9
9/7/22, 5:07 PM Python - Multithreaded Programming

run() − The run() method is the entry point for a thread.

start() − The start() method starts a thread by calling the run method.
join([time]) − The join() waits for threads to terminate.

isAlive() − The isAlive() method checks whether a thread is still executing.


getName() − The getName() method returns the name of a thread.

setName() − The setName() method sets the name of a thread.

Creating Thread Using Threading Module


To implement a new thread using the threading module, you have to do the following −

Define a new subclass of the Thread class.

Override the __init__(self [,args]) method to add additional arguments.


Then, override the run(self [,args]) method to implement what the thread should do when
started.

Once you have created the new Thread subclass, you can create an instance of it and then start
a new thread by invoking the start(), which in turn calls run() method.

Example
#!/usr/bin/python

import threading

import time

exitFlag = 0

class myThread ([Link]):

def __init__(self, threadID, name, counter):

[Link].__init__(self)

[Link] = threadID

[Link] = name

[Link] = counter

def run(self):

print "Starting " + [Link]

print_time([Link], 5, [Link])

print "Exiting " + [Link]

def print_time(threadName, counter, delay):

while counter:

if exitFlag:

[Link]()

[Link] 4/9
9/7/22, 5:07 PM Python - Multithreaded Programming

[Link](delay)

print "%s: %s" % (threadName, [Link]([Link]()))

counter -= 1

# Create new threads

thread1 = myThread(1, "Thread-1", 1)

thread2 = myThread(2, "Thread-2", 2)

# Start new Threads

[Link]()

[Link]()

print "Exiting Main Thread"

When the above code is executed, it produces the following result −

Starting Thread-1

Starting Thread-2

Exiting Main Thread

Thread-1: Thu Mar 21 09:10:03 2013

Thread-1: Thu Mar 21 09:10:04 2013

Thread-2: Thu Mar 21 09:10:04 2013

Thread-1: Thu Mar 21 09:10:05 2013

Thread-1: Thu Mar 21 09:10:06 2013

Thread-2: Thu Mar 21 09:10:06 2013

Thread-1: Thu Mar 21 09:10:07 2013

Exiting Thread-1

Thread-2: Thu Mar 21 09:10:08 2013

Thread-2: Thu Mar 21 09:10:10 2013

Thread-2: Thu Mar 21 09:10:12 2013

Exiting Thread-2

Synchronizing Threads
The threading module provided with Python includes a simple-to-implement locking mechanism
that allows you to synchronize threads. A new lock is created by calling the Lock() method, which
returns the new lock.

The acquire(blocking) method of the new lock object is used to force threads to run
synchronously. The optional blocking parameter enables you to control whether the thread waits
to acquire the lock.

If blocking is set to 0, the thread returns immediately with a 0 value if the lock cannot be acquired
and with a 1 if the lock was acquired. If blocking is set to 1, the thread blocks and wait for the
lock to be released.

[Link] 5/9
9/7/22, 5:07 PM Python - Multithreaded Programming

The release() method of the new lock object is used to release the lock when it is no longer
required.

Example
#!/usr/bin/python

import threading

import time

class myThread ([Link]):

def __init__(self, threadID, name, counter):

[Link].__init__(self)

[Link] = threadID

[Link] = name

[Link] = counter

def run(self):

print "Starting " + [Link]

# Get lock to synchronize threads

[Link]()

print_time([Link], [Link], 3)

# Free lock to release next thread

[Link]()

def print_time(threadName, delay, counter):

while counter:

[Link](delay)

print "%s: %s" % (threadName, [Link]([Link]()))

counter -= 1

threadLock = [Link]()

threads = []

# Create new threads

thread1 = myThread(1, "Thread-1", 1)

thread2 = myThread(2, "Thread-2", 2)

# Start new Threads

[Link]()

[Link]()

# Add threads to thread list

[Link](thread1)

[Link](thread2)

# Wait for all threads to complete

[Link] 6/9
9/7/22, 5:07 PM Python - Multithreaded Programming

for t in threads:

[Link]()

print "Exiting Main Thread"

When the above code is executed, it produces the following result −

Starting Thread-1

Starting Thread-2

Thread-1: Thu Mar 21 09:11:28 2013

Thread-1: Thu Mar 21 09:11:29 2013

Thread-1: Thu Mar 21 09:11:30 2013

Thread-2: Thu Mar 21 09:11:32 2013

Thread-2: Thu Mar 21 09:11:34 2013

Thread-2: Thu Mar 21 09:11:36 2013

Exiting Main Thread

Multithreaded Priority Queue


The Queue module allows you to create a new queue object that can hold a specific number of
items. There are following methods to control the Queue −

get() − The get() removes and returns an item from the queue.

put() − The put adds item to a queue.


qsize() − The qsize() returns the number of items that are currently in the queue.

empty() − The empty( ) returns True if queue is empty; otherwise, False.


full() − the full() returns True if queue is full; otherwise, False.

Example
#!/usr/bin/python

import Queue

import threading

import time

exitFlag = 0

class myThread ([Link]):

def __init__(self, threadID, name, q):

[Link].__init__(self)

[Link] = threadID

[Link] = name

self.q = q

def run(self):

[Link] 7/9
9/7/22, 5:07 PM Python - Multithreaded Programming

print "Starting " + [Link]

process_data([Link], self.q)

print "Exiting " + [Link]

def process_data(threadName, q):

while not exitFlag:

[Link]()

if not [Link]():

data = [Link]()

[Link]()

print "%s processing %s" % (threadName, data)

else:

[Link]()

[Link](1)

threadList = ["Thread-1", "Thread-2", "Thread-3"]

nameList = ["One", "Two", "Three", "Four", "Five"]

queueLock = [Link]()

workQueue = [Link](10)

threads = []

threadID = 1

# Create new threads

for tName in threadList:

thread = myThread(threadID, tName, workQueue)

[Link]()

[Link](thread)

threadID += 1

# Fill the queue

[Link]()

for word in nameList:

[Link](word)

[Link]()

# Wait for queue to empty

while not [Link]():

pass

# Notify threads it's time to exit

exitFlag = 1

# Wait for all threads to complete

for t in threads:

[Link]()

print "Exiting Main Thread"

[Link] 8/9
9/7/22, 5:07 PM Python - Multithreaded Programming

When the above code is executed, it produces the following result −

Starting Thread-1

Starting Thread-2

Starting Thread-3

Thread-1 processing One

Thread-2 processing Two

Thread-3 processing Three

Thread-1 processing Four

Thread-2 processing Five

Exiting Thread-3

Exiting Thread-1

Exiting Thread-2

Exiting Main Thread

[Link] 9/9

Common questions

Powered by AI

The `join()` method in Python's multithreaded programming is used to block the calling thread until the thread whose `join()` method is called is terminated. This ensures that a thread waits for another thread to complete its execution before proceeding. When a timeout argument is provided, the join operation is blocked at most until the thread terminates or the specified timeout occurs. This is crucial in situations where thread execution order is important or when the main program needs to pause until all other threads finish. It enhances control over thread lifecycles, ensuring that resources are managed effectively and any needed synchronization is applied .

To create a new thread subclass using Python's threading module, follow these steps: 1) Define a new subclass that inherits from `threading.Thread`. 2) Override the `__init__(self, *args, **kwargs)` method to initialize the thread object's properties. Arguments can be passed to the thread during its initialization. 3) Override the `run(self)` method, which will contain the code that will be executed in the new thread. 4) Create an instance of this subclass. 5) Call the `start()` method on the instance to begin thread execution, which will internally call the `run()` method .

Locks play a crucial role in synchronizing threads by preventing simultaneous access to a shared resource, avoiding race conditions. In Python, locks are implemented using the threading module. A lock is created by calling the `Lock()` method, which returns a new lock object. The `acquire()` method is used to gain control of the lock and prevent other threads from accessing the shared resource. If `acquire()` is called with `blocking` set to 0, it returns immediately with a false value if the lock cannot be acquired, otherwise, with a true value. The `release()` method is used to free the lock, allowing other threads to acquire it .

In Python's threading module, the `acquire()` method of the lock object is used to obtain a lock, which synchronizes thread access to a shared resource. The optional 'blocking' parameter determines the method's behavior when the lock is unavailable. If 'blocking' is set to 1 (default), the calling thread will block and wait until the lock is released. If set to 0, the thread will not wait and will immediately return false if the lock is unavailable, otherwise, true if the lock is acquired. This allows more control over thread synchronization, enabling threads to handle lock acquisition non-blocking or by waiting as needed .

In Python, the threading module can be combined with the queue module to manage tasks using a Priority Queue. A new queue object can be created using `Queue.Queue(maxsize)`, where 'maxsize' indicates the maximum number of items the queue can hold. Methods like `put()` to add an item and `get()` to remove an item are used to manage queue operations. The process involves first acquiring a lock with `queueLock.acquire()` to prevent concurrent access to the queue, checking if the queue is empty using `empty()`, and performing operations like adding data with `put()`. Threads remove data from the queue when available using `get()`, and once finished with the task, the queue lock is released. This approach allows effective multi-threaded task management with threads processing tasks in order of priority .

In Python's threading module, the `start()` method is responsible for initiating a thread. It sets up the thread to be run, and when called, `start()` calls the `run()` method of the thread object, creating a new thread of execution. The `run()` method, on the other hand, is the entry point for a thread. It defines the operations that the thread should perform when it starts. By default, calling `run()` does not start a new thread but executes the method in the current thread's context, which is why `run()` should not be directly invoked to initiate threading. Instead, `start()` should be used .

The thread module is considered limited compared to the threading module because it offers only basic thread handling capabilities without higher-level abstractions. It lacks support for critical threading features such as easy thread management and state inspection. on the other hand, provides extensive threading support, including higher-level threading classes and methods such as `Thread`, `currentThread()`, `activeCount()`, `enumerate()`, and other synchronization primitives like RLock and Semaphores. These features make the threading module more versatile and efficient for advanced thread management and synchronization tasks .

The 'thread' module provides low-level primitives for working with threads but is limited compared to the 'threading' module. The 'threading' module, introduced in Python 2.4, offers more powerful and high-level support for threads. It exposes additional methods such as `threading.activeCount()`, `threading.currentThread()`, and `threading.enumerate()` which help manage threads in a program more effectively. Furthermore, the 'threading' module includes the Thread class, allowing users to create thread objects and implement additional methods such as `run()`, `join()`, and `isAlive()`. These improvements make the 'threading' module more suitable for complex multithreaded programming since it provides more control and better support mechanisms .

To implement a priority queue in a multithreaded Python program, you typically use the queue module to create a PriorityQueue instance. Essential methods for the operation of a priority queue include `put()` to add an item to the queue and `get()` to remove an item. The `qsize()` method returns the number of items in the queue, while the `empty()` and `full()` methods determine if the queue is empty or full, respectively. These methods enable efficient task management among threads by ensuring that items are processed based on their priority order. A lock mechanism is usually employed to synchronize access to the queue, utilizing methods like `acquire()` and `release()` to manage concurrent thread access effectively .

To create and synchronize threads using locks in Python's threading module, follow these steps: 1) Define a subclass of the 'Thread' class in the threading module. 2) Override the `__init__()` method to initialize thread attributes and the `run()` method to define its operations. 3) Create a lock object using `threading.Lock()`. 4) In `run()`, use `lock.acquire()` before accessing shared resources to ensure synchronization. 5) Perform the required operations. 6) Use `lock.release()` to release the lock after the operations. 7) Start the threads using the `start()` method. By following these steps, you ensure that threads access shared resources safely without conflict .

(https://www.tutorialspoint.com/practical-python-learn-python-basics-step-by-step-python-3/index.asp) (https://www.tutorials
9/7/22, 5:07 PM
Python - Multithreaded Programming
https://www.tutorialspoint.com/python/python_multithreading.htm
2/9
Runnin
9/7/22, 5:07 PM
Python - Multithreaded Programming
https://www.tutorialspoint.com/python/python_multithreading.htm
3/9
9/7/22, 5:07 PM
Python - Multithreaded Programming
https://www.tutorialspoint.com/python/python_multithreading.htm
4/9
run()
9/7/22, 5:07 PM
Python - Multithreaded Programming
https://www.tutorialspoint.com/python/python_multithreading.htm
5/9
9/7/22, 5:07 PM
Python - Multithreaded Programming
https://www.tutorialspoint.com/python/python_multithreading.htm
6/9
The re
9/7/22, 5:07 PM
Python - Multithreaded Programming
https://www.tutorialspoint.com/python/python_multithreading.htm
7/9
for t
9/7/22, 5:07 PM
Python - Multithreaded Programming
https://www.tutorialspoint.com/python/python_multithreading.htm
8/9
9/7/22, 5:07 PM
Python - Multithreaded Programming
https://www.tutorialspoint.com/python/python_multithreading.htm
9/9
When t

You might also like