 A program under execution is called a
process.
 Every process has an Id.
 To get the id of a process, we can use getpid
function from os module.
 Every process also has a parent process.
 To get the id of parent process, we can use
getppid() function from os module.
Multiprocessing refers to the ability of a
system to support more than one
process(running program) at the same time.
 Entire code is executed in single process
 To create a process, we create an instance
of Process class and pass the target function.
 To start a process, we use start method.
 To wait for the child process to
complete, use join method.
 We can pass arguments using the args
parameter.
 args accepts tuple as argument
 Process synchronization is defined as a mechanism which
ensures that two or more concurrent processes 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.
 Concurrent accesses to shared resource can lead to race
condition.
 A race condition occurs when two or more
processes 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.
 multiprocessing module provides a Lock class
to deal with the race conditions.
 Lock is implemented using a
Semaphore object provided by the OS .
 A thread is a single sequence of execution
within a process.
 Each process can run multiple threads.
 Simplify code that deals with asynchronous
events.
 Problems can be partitioned so that overall
program throughput can be improved.
 Interactive programs can realize improved
response time.
 The Thread class represents an activity that
is run in a separate thread of control.
 We can specify the activity by passing a
callable object to the constructor.
 Once a thread object is created, its activity
must be started by calling the
thread’s start() method.
 Threading.enumerate function returns list of
allThread objects currently alive.
Multiprocessing.pptx
Multiprocessing.pptx
Multiprocessing.pptx
Multiprocessing.pptx

Multiprocessing.pptx

  • 2.
     A programunder execution is called a process.  Every process has an Id.  To get the id of a process, we can use getpid function from os module.
  • 3.
     Every processalso has a parent process.  To get the id of parent process, we can use getppid() function from os module.
  • 4.
    Multiprocessing refers tothe ability of a system to support more than one process(running program) at the same time.
  • 5.
     Entire codeis executed in single process
  • 7.
     To createa process, we create an instance of Process class and pass the target function.  To start a process, we use start method.  To wait for the child process to complete, use join method.
  • 10.
     We canpass arguments using the args parameter.  args accepts tuple as argument
  • 15.
     Process synchronizationis defined as a mechanism which ensures that two or more concurrent processes 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.  Concurrent accesses to shared resource can lead to race condition.
  • 16.
     A racecondition occurs when two or more processes 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.
  • 18.
     multiprocessing moduleprovides a Lock class to deal with the race conditions.  Lock is implemented using a Semaphore object provided by the OS .
  • 21.
     A threadis a single sequence of execution within a process.  Each process can run multiple threads.
  • 23.
     Simplify codethat deals with asynchronous events.  Problems can be partitioned so that overall program throughput can be improved.  Interactive programs can realize improved response time.
  • 25.
     The Threadclass represents an activity that is run in a separate thread of control.  We can specify the activity by passing a callable object to the constructor.  Once a thread object is created, its activity must be started by calling the thread’s start() method.
  • 31.
     Threading.enumerate functionreturns list of allThread objects currently alive.

Editor's Notes

  • #2 https://docs.python.org/3.5/library/multiprocessing.html#contexts-and-start-methods
  • #6 Entire code is running in single process. ----------------------------------------------------------------------------------- import os   def myfunc1():     print("MyFunction-1 - The process id = " , os.getpid())     print("MyFunction-1 - The parent process id = " , os.getppid())   def myfunc2():     print("MyFunction-2 - The process id = " , os.getpid())     print("MyFunction-2 - The parent process id = " , os.getppid())   if __name__ == "__main__":     print("Main - The process id = " , os.getpid())     print("Main - The parent process id = " , os.getppid())       myfunc1();     myfunc2();             
  • #7 Total time taken will be approx 10 sec As its sequential. We cannot start myfunc2 until we complete myfunc1. ------------------------------------------------------------------------------------  import time import os   def myfunc1():     for i in range(5):         print('*')         time.sleep(1)   def myfunc2():     for i in range(5):         print('-')         time.sleep(1)          if __name__ == "__main__":     start = time.time()       myfunc1();     myfunc2();       end = time.time()     print("Total time taken = " , end - start)
  • #8 Once the new process starts, the current program also keeps on executing. In order to stop execution of current program until a process is complete, we use join method.
  • #10   import time import os import multiprocessing as mp   def myfunc1():     for i in range(5):                print('*')         time.sleep(1)   def myfunc2():     for i in range(5):         print('-')         time.sleep(1)          if __name__ == "__main__":        #Creating Process     p1 = mp.Process(target = myfunc1)     p2 = mp.Process(target = myfunc2)          start = time.time()     p1.start()     p2.start()          p1.join()     p2.join()       end = time.time()     print("Total time taken = " , end - start)
  • #11 import collections as c a = [1, 4, 4, 4, 5, 3] freq = c.Counter(a) print(freq)
  • #12   import time import os import multiprocessing as mp   def myfunc1(num):     for i in range(num):                print(i, '*')         time.sleep(1)   def myfunc2(num):     for i in range(num):         print(i,'-')         time.sleep(1)   if __name__ == "__main__":        p1 = mp.Process(target = myfunc1, args = (5,) )     p2 = mp.Process(target = myfunc2 , args = (8,) )              start = time.time()     p1.start()     p2.start()          p1.join()     p2.join()        end = time.time()     print("Total time taken = " , end - start)     
  • #13   import time import os import multiprocessing as mp   def myfunc1(c,num):          for i in range(num):                print(i, c , os.getpid())         time.sleep(1)   if __name__ == "__main__":        p1 = mp.Process(target = myfunc1, args = ('*',5) )     p2 = mp.Process(target = myfunc1 , args = ('-',8) )              start = time.time()     p1.start()     p2.start()          p1.join()     p2.join()        end = time.time()     print("Total time taken = " , end - start)
  • #14   import os import multiprocessing as mp   value = 5   def update():     global value     value += 5     print("Update - value = " , value)   if __name__ == "__main__":          update()                print("Main - value = " , value)
  • #15 #Program to demonstrate – parent and child process runs in separate memory     import os import multiprocessing as mp   value = 5   def update():     global value     value += 5     print("Update - value = " , value)   if __name__ == "__main__":          p1 = mp.Process(target = update )       p1.start()     p1.join()               print("Main - value = " , value)           
  • #17 https://www.geeksforgeeks.org/synchronization-pooling-processes-python/
  • #19 A semaphore is a synchronization object that controls access by multiple processes to a common resource in a parallel programming environment. It is simply a value in a designated place in operating system (or kernel) storage that each process can check and then change. Depending on the value that is found, the process can use the resource or will find that it is already in use and must wait for some period before trying again. Semaphores can be binary (0 or 1) or can have additional values. Typically, a process using semaphores checks the value and then, if it using the resource, changes the value to reflect this so that subsequent semaphore users will know to wait.
  • #20 # Python program to illustrate the concept of locks in multiprocessing import multiprocessing as mp   def withdraw(balance, lock):         for _ in range(10000):     lock.acquire()     balance.value = balance.value - 1     lock.release()   def deposit(balance, lock):         for _ in range(10000):     lock.acquire()     balance.value = balance.value + 1     lock.release()   def perform_transactions():         balance = mp.Value('i', 100)          lock = mp.Lock()          p1 = mp.Process(target=withdraw, args=(balance,lock))     p2 = mp.Process(target=deposit, args=(balance,lock))       p1.start()     p2.start()          p1.join()     p2.join()         print("Final balance = ",balance.value)   if __name__ == "__main__":     perform_transactions()
  • #25  C:\sikander>python thread1.py Process ID :  11880 Thread ID :  9196 Number of threads :  1 ======================================================== import os import threading    if __name__ == "__main__":     print("Process ID : ",os.getpid())     print("Thread ID : ",threading.get_ident())     print("Number of threads : ",threading.active_count())     
  • #27  C:\sikander>python thread2_creating_thread.py Main - Process ID :  11532 Main - Thread ID :  32 Number of threads :  1 Func - Process ID :  11532 Number of threads :  2 Func - Thread ID :  4188 Thread finished   Number of threads :  1 ============================================================ import os import threading    def myfunc():     print("Func - Process ID : ",os.getpid())     print("Func - Thread ID : ",threading.get_ident())      if __name__ == "__main__":     print("Main - Process ID : ",os.getpid())     print("Main - Thread ID : ",threading.get_ident())     print("Number of threads : ",threading.activeCount())              t1 = threading.Thread(target = myfunc)     t1.start()          print("Number of threads : ",threading.activeCount())       t1.join()     print("Thread finished \n")     print("Number of threads : ",threading.activeCount())
  • #28  C:\sikander>python thread3_parallel_execution.py Number of threads :  1 * - Number of threads :  3 * - * - * - * - Total time taken =  5.0071704387664795 ====================================================== import time import os import threading    def myfunc1():     for i in range(5):                 print('*')         time.sleep(1)   def myfunc2():     for i in range(5):         print('-')         time.sleep(1)   if __name__ == "__main__":     print("Number of threads : ",threading.activeCount())     t1 = threading.Thread(target = myfunc1)     t2 = threading.Thread(target = myfunc2)          start = time.time()     t1.start()     t2.start()     print("Number of threads : ",threading.activeCount())     t1.join()     t2.join()       end = time.time()     print("Total time taken = " , end - start)  
  • #29  Data Segment is shared between multiple threads import threading    value = 5   def update():     global value     value += 5     print("Update - value = " , value)   if __name__ == "__main__":          t1 = threading.Thread(target = update )       t1.start()     t1.join()               print("Main - value = " , value)  
  • #30   import time import os import threading    def myfunc1(n):     for i in range(n):                 print('*')         time.sleep(1)   def myfunc2(n):     for i in range(n):         print('-')         time.sleep(1)   if __name__ == "__main__":     t1 = threading.Thread(target = myfunc1, args = (5,) )     t2 = threading.Thread(target = myfunc2, args = (8,) )       t1.start()     t2.start()       t1.join()     t2.join()    
  • #33 1. Modify the number of iterations in myfunc2 to < 5 ========================================================== C:\sikander>python thread_enumerate.py [<_MainThread(MainThread, started 7740)>] * - [<_MainThread(MainThread, started 7740)>, <Thread(Thread-1, started 8684)>, <Thread(Thread-2, started 7736)>] * - * - * - * - [<_MainThread(MainThread, started 7740)>, <Thread(Thread-2, started 7736)>] - - - [<_MainThread(MainThread, started 7740)>]   =========================================================================== import threading import time   def myfunc1():     for i in range(5):                print('*')         time.sleep(1)   def myfunc2():     for i in range(8):         print('-')         time.sleep(1)   if __name__ == "__main__":        t1 = threading.Thread(target = myfunc1)     t2 = threading.Thread(target = myfunc2)     print(threading.enumerate())       t1.start()     t2.start()       print(threading.enumerate())       t1.join()        print(threading.enumerate())         t2.join()     print(threading.enumerate())         '''     print(threading.currentThread())     #print(t2.get_ident())     main_thread = threading.currentThread()         for t in threading.enumerate():         if t is not main_thread:             t.join() '''      
  • #34   import threading import time   def myfunc1():     for i in range(5):                print('*')         time.sleep(1)   def myfunc2():     for i in range(8):         print('-')         time.sleep(1)   if __name__ == "__main__":        t1 = threading.Thread(target = myfunc1)     t2 = threading.Thread(target = myfunc2)         t1.start(); t2.start()     print(threading.enumerate())       main_thread = threading.currentThread()         for t in threading.enumerate():         if t is not main_thread:             t.join()       print(threading.enumerate())     print("All threads completed")
  • #35   import threading import time   data = 5   def increment():     global data          temp = data     time.sleep(1)     temp = temp + 1     data = temp   if __name__ == "__main__":        t1 = threading.Thread(target = increment)     t2 = threading.Thread(target = increment)          t1.start(); t2.start()              main_thread = threading.currentThread()          for t in threading.enumerate():         if t is not main_thread:             t.join()       print("Updated value of data : " , data)