fork download
  1. import time
  2. from os import getpid
  3.  
  4. def init_pool_processes():
  5. global the_time
  6. the_time = time.time()
  7.  
  8. def testfun(i):
  9. # Ensure each task is run by a different process:
  10. time.sleep(.2)
  11. print(f'Process {getpid()}, the_time = {the_time}')
  12.  
  13. # Required by Windows:
  14. if __name__ == '__main__':
  15. from multiprocessing import Pool
  16.  
  17. pool = Pool(processes=8, initializer=init_pool_processes)
  18.  
  19. pool.map(testfun, range(8))
Success #stdin #stdout 0.1s 15004KB
stdin
Standard input is empty
stdout
Process 20655, the_time = 1666182035.6631467
Process 20656, the_time = 1666182035.6641986
Process 20657, the_time = 1666182035.6652298
Process 20658, the_time = 1666182035.6662962
Process 20650, the_time = 1666182035.6673143
Process 20652, the_time = 1666182035.6683464
Process 20654, the_time = 1666182035.6693463
Process 20653, the_time = 1666182035.6700563