Multithreading in Python
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 .
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.
What is
• Kernel does the thread
management.
• Supported directly by the
Operating System.
(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.
• 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
Creating a
def worker(num):
print("worker {}".format(num))
thread time.sleep(5)
return
without
creating a threads = []
without print("Child")
extending myobj=threadclass()
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.
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)