Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
74 views

Multithreading in Python

Multithreading in Python allows a program to have multiple threads running concurrently by using threading or multiprocessing modules, where threads allow exploiting idle CPU time within a process and multiprocessing allows utilizing multiple processors. Threads can be created by extending the Thread class, without extending the class, or without creating a class at all and synchronization techniques like locks are needed to prevent race conditions when accessing shared resources.

Uploaded by

digi ocean
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Multithreading in Python

Multithreading in Python allows a program to have multiple threads running concurrently by using threading or multiprocessing modules, where threads allow exploiting idle CPU time within a process and multiprocessing allows utilizing multiple processors. Threads can be created by extending the Thread class, without extending the class, or without creating a class at all and synchronization techniques like locks are needed to prevent race conditions when accessing shared resources.

Uploaded by

digi ocean
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Multithreading in Python

Concurrent Execution
• Concurrency is the occurrence of two or more events at the same time.
• With concurrent programming, the performance of software is improved
because one task doesn’t have to wait for the completion of another task.
• The most common mechanisms supporting concurrency are variations of
one of the following:
• Multiprocessing — multiple CPUs executing concurrently
• Multitasking — the operating systems simulates concurrency on a
single CPU by interleaving the execution of different tasks
• Application-based solutions — the application takes responsibility for
switching between different branches of code at appropriate times
Multitasking, is the concurrent execution of
multiple tasks .

In this technique, the multiple tasks, also known as


Multitasking processes, share common processing resources such as a
CPU.

There are two types of multitasking in an OS:

Process-based Thread-based
Refers to the ability of a system to
support more than one processor at
the same time.

Applications in a multiprocessing
Multiprocessing system are broken to smaller routines
that run independently.

The operating system allocates these


threads to the processors improving
performance of the system.
Is a way of achieving multitasking.

Is the ability of a CPU to manage the use of operating system


by executing multiple threads concurrently.

On a single processor machine, this is achieved by thread


Multithreading scheduling or time slicing.

Multitasking automatically interrupts the running program, saving


its state and loading the saved state of another program and
transferring control to it.
This context switch may be initiated at fixed time intervals (pre-
emptive multitasking), or the running program may signal the
supervisory software when it can be interrupted (cooperative
multitasking)
• A thread is an entity within a process
that can be scheduled for execution.

• It is the smallest unit of processing


that can be performed in an

What is a Operating System.

thread? • More than one thread can exist


within the same process. These
threads share the memory and the
state of the process.

• Each thread in a program performs


a particular task.
• There are two different kind of
threads:
• Kernel threads

What is
• Kernel does the thread
management.
• Supported directly by the
Operating System.

a thread? • User threads


• User threads are not
implemented in the kernel.

(cont.)
• The thread library contains code
for creating and destroying
threads, for communication
between threads, thread
scheduling and for saving and
restoring thread contexts.
Multithreaded Processes
Differences between Processes and Threads

Process Thread
Process is resource intensive. Thread is called lightweight since takes fewer resources
than a process.
Process switching needs interaction with operating system. Thread switching does not need to interact with operating
system.
In multiple processing environments, each process executes the All threads can share same set of open files, child
same code but has its own memory and file resources. processes.
In multiple processes, each process operates independently of the One thread can read, write or change another thread's
others. data.
To communicate with sibling processes, processes must use inter- Threads can directly communicate with other threads of
process communication. that process.
Process has its own Process Control Block, Stack and Address Thread has Parents’ PCB, its own Thread Control Block and
Space. Stack and common Address space.
Multithreading improves the speed of computation.

It allows a program to remain responsive without getting


blocked, for example, one thread waits for the input and
another does some computation.

Advantages of The threads of a particular process can access the global


variables and any change in global variables is visible to other
Multithreading threads too.

Running of several threads in each program makes better use


of CPU and the idle time of CPU becomes less.

There is no requirement of extra space for each thread


because threads within a program can share same data.
It is difficult to achieve performance in terms of speed
of computation on single processor system as
compared to that on multi-processor system.
Security can be an issue since all threads share data,
any unknown thread can change the data.

Disadvantages Multithreading can increase the complexity of the


of program and debugging becomes difficult.
Multithreading
Multithreading can lead the program to potential risk
of attaining the deadlock state.

Synchronization is required to avoid mutual exclusion.


This leads to more memory and CPU utilization.
• Python has many packages to handle
multi tasking:

• os.system
• Runs a command in the shell 

Multitasking • os.popen
in Python • Run a process and read its output as a file

• subprocess module
• allows you to spawn new processes, connect to
their input/output/error pipes, and obtain their
return codes.
• Intends to replace several older modules and
functions
Threading module

• Constructs higher-level threading


interfaces on top of the lower
level _thread module.
• _thread module treats a thread as a
Multitasking function whereas, the threading module
treats every thread as an object
in Python
(Cont.) Multiprocessing module

• The multiprocessing module is suitable for


sharing data or tasks between processor
cores. It does not use threading, but
processes instead.
• Threads in Python can be created in
Creating three ways:
• Without creating a class
threads in • By extending Thread class
Python • Without extending Thread class
import threading
import time

Creating a
def worker(num):
print("worker {}".format(num))
thread time.sleep(5)
return
without
creating a threads = []

class for i in range(5):


t = threading.Thread(target= worker, args = (i,))
threads.append(t)
t.start()
from threading import *
class threadclass:
Create a def mythread(self):
thread for x in range(7):

without print("Child")

extending myobj=threadclass()

the Thread thread1=Thread(target=myobj.mythread)

class thread1.start()
thread1.join()
print("done“)
Impomrt threading
class MyThread(Thread):
def run(self):
Create a # code goes here
thread by
extending newthread = MyThread()
the Thread newthread.start()
class newthread.join()
Synchronization
• Thread synchronization is defined as a
mechanism which ensures that two or
more concurrent threads do not
simultaneously execute some particular
program segment known as critical section.
• Critical section refers to the parts of the
program where the shared resource is
accessed.
Synchronizing threads (Cont.)

• A race condition occurs when two or more threads can access shared data
and they try to change it at the same time. As a result, the values of
variables may be unpredictable and vary depending on the timings of
context switches of the processes.

• threading module provides a Lock class to deal with the race conditions.


Lock is implemented using a Semaphore object provided by the Operating
System.
• The concurrent.futures module provides a
high-level interface for asynchronously
executing callables.

• The asynchronous execution can be


Concurrent.futures performed with threads, using
ThreadPoolExecutor, or separate
processes, using ProcessPoolExecutor.
Both implement the same interface, which
is defined by the abstract Executor class.
An Example
from pathlib import Path with open(fname, "wb") as f:
import urllib.request while True:
from concurrent.futures import ThreadPoolExecutor, chunk = req.read(1024)
as_completed
if not chunk:
break
def download_one(url):
f.write(chunk)
req = urllib.request.urlopen(url)
fullpath = Path(url)
msg = f"Finished downloading {fname}"
fname = fullpath.name
return msg
ext = fullpath.suffix

def download_all(urls):
with ThreadPoolExecutor(max_workers=13) as executor:
return executor.map(download_one, urls, timeout=60)
Example (Cont.)

if __name__ == "__main__":
urls = (
#Some URLs
)

results = download_all(urls)
for result in results:
print(result)
To handle the Exception….
with ThreadPoolExecutor(max_workers=13) as executor:
for url in urls:
futures = executor.submit(download_one, url)
futures_list.append(futures)

for future in futures_list:


try:
result = future.result(timeout=60)
results.append(result)
except Exception:
results.append(None)
return results

You might also like