Guest User

Untitled

a guest
Nov 25th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # This example shows how to launch multiple processes in parallel on a single machine with multiple cpus
  4. # There is no communication between processes and no data are gathered in the end.
  5. # Each process executes the same function but with different arguments.
  6. # The script wait for all sub-processes to be done, then execute another task.
  7. #
  8. # How to run on your computer:
  9. # python multiprocessing_eg_02.py
  10. #
  11. # How to run on datarmor:
  12. # qsub -q sequentiel -N test_pythonpar launch_this.sh
  13. #
  14. # The launcher script is: launch_this.sh with:
  15. # ======================================================================
  16. # #!/bin/bash
  17. # source ~/.bashrc
  18. # source activate obidam
  19. # python /home1/datahome/gmaze/work/Play_Ground/python/parallel/examples/multiprocessing_eg_02datarmor.py
  20. # exit 0
  21. # ======================================================================
  22. #
  23. # Eg:
  24. # >> qsub -q sequentiel -N test_python multiprocessing_eg_02.py
  25. # >> qstat -u gmaze
  26. #
  27. # datarmor0:
  28. # Req'd Req'd Elap
  29. # Job ID Username Queue Jobname SessID NDS TSK Memory Time S Time
  30. # --------------- -------- -------- ---------- ------ --- --- ------ ----- - -----
  31. # 508731.datarmor gmaze ice_1t test_pytho 23442 1 1 500mb 00:05 R 00:00
  32. # r2i2n10/4
  33. #
  34. # >> more test_pythonpar.o508731
  35. # ======================================================================
  36. # multiprocessing_eg_02.py.
  37. # This machine has 56 cores.
  38. # ======================================================================
  39. #
  40. # - Process named: dummy_name_1 (running on r2i2n10) just started.
  41. # It received argument:
  42. # 61
  43. #
  44. # - Process named: dummy_name_2 (running on r2i2n10) just started.
  45. # It received argument:
  46. # 73
  47. #
  48. # - Process named: dummy_name_7 (running on r2i2n10) just started.
  49. # It received argument:
  50. # 33
  51. # [...]
  52. #
  53. # End of the main program (this text will be displayed after all processes finished)
  54. #
  55.  
  56. # Created: 2013-02-06.
  57. # Copyright (c) 2013, Guillaume Maze (Ifremer, Laboratoire de Physique des Oceans).
  58. # Revision by G. Maze on 2017-11-23: Added help for datarmor
  59. # All rights reserved.
  60.  
  61. import sys, socket, time
  62. import multiprocessing
  63. import numpy as np
  64.  
  65. def worker(num):
  66. """
  67. A worker function: defines the task to be performed by a process.
  68. This function is executed by each process with a
  69. different argument 'num'
  70. """
  71. # Retrieve the name of this process:
  72. procname = multiprocessing.current_process().name
  73. # Retrive the name of the machine:
  74. hostname = socket.gethostname()
  75.  
  76. # Print out some usefull info:
  77. msg = "\t- Process named: %s (running on %s) just started.\n\tIt received argument:\n\t%i\n\n"
  78. sys.stdout.write(msg % (procname,hostname,num))
  79.  
  80. # Dummy action, simple wait for anywhere between 4 and 5 seconds
  81. time.sleep(np.random.randint(4,5,1))
  82.  
  83.  
  84. if __name__ == '__main__':
  85.  
  86. # How many core have this machine ?
  87. N_CORES = multiprocessing.cpu_count()
  88. print ('%s\nmultiprocessing_eg_02.py.\nThis machine has %i cores.\n%s\n')%("="*70,N_CORES,"="*70)
  89.  
  90. # Init the list which is going to hold the list of processes:
  91. process_list = []
  92.  
  93. # Create a dummy list of parameters.
  94. param_list = np.random.randint(20,100,N_CORES)
  95.  
  96. # Now we will start N_CORES processes.
  97. # All will run the 'worker' function with a dummy parameter from 'param_list'
  98. for i in range(N_CORES):
  99. # Define a process:
  100. p = multiprocessing.Process(target=worker, args=(param_list[i],), name=('dummy_name_%i')%(i))
  101. # Start the process:
  102. p.start()
  103. # Register the process in a list
  104. process_list.append(p)
  105.  
  106. # Now we tell the script to wait for all processes to finish before moving on
  107. for p in process_list:
  108. p.join()
  109.  
  110. # This won't be executed before the end of all processes:
  111. print "End of the main program (this text will be displayed after all processes finished)\n"
Add Comment
Please, Sign In to add comment