gautamkrishnar

Multiprocessing in python

Jul 12th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. import multiprocessing as mp
  2. process_list = []
  3.  
  4. def pr1(test,test1):
  5.     """
  6.    @:param: test,test1 (Integer)
  7.    process 1
  8.    """
  9.     print("Pr 1 prining... '"+str(test)+"','"+str(test1)+"' recieved...")
  10.  
  11. def pr2():
  12.     """
  13.    process 2
  14.    """
  15.     print("Pr2 2 prining...")
  16.  
  17. def pr3():
  18.     """
  19.    process 3
  20.    """
  21.     print("Pr3 3 prining...")
  22.  
  23.  
  24.  
  25. def initprocess(function_name,args=()):
  26.     """
  27.    @:param function_name: name of the function
  28.    @:param arg: Arguments to pass to function (tuple)
  29.  
  30.    Init processes and add it to the process list
  31.    t"""
  32.     global process_list
  33.     if args == ():
  34.         process_list.append(mp.Process(target=function_name))
  35.     else:
  36.         process_list.append(mp.Process(target=function_name,args=args))
  37.  
  38.     return
  39.  
  40.  
  41.  
  42. def run():
  43.     global process_list
  44.     for process in process_list:
  45.         process.start()
  46.  
  47. def main():
  48.     initprocess(pr1,(1,2,)) #Function with arguments
  49.     initprocess(pr2) #Function without arguments
  50.     initprocess(pr3)
  51.     run()
  52.  
  53.  
  54. if __name__ == '__main__':
  55.     main()
Add Comment
Please, Sign In to add comment