Guest User

Untitled

a guest
May 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. from multiprocessing import Pool
  2.  
  3.  
  4. def write_file(file: str):
  5. f = open(file, 'w')
  6. for item in range(0, 1500000):
  7. f.write("%sn" % item)
  8. f.close()
  9.  
  10.  
  11. if __name__ == '__main__':
  12. list_files = ['1.txt', '2.txt', '3.txt']
  13. with Pool(3) as p:
  14. p.map(write_file, list_files)
  15.  
  16. from multiprocessing import Process
  17. import time
  18. pdict = {}
  19. for fname in list_files:
  20. p = Process(target = write_file, args = (fname,))
  21. pdict[fname] = p
  22. p.start()
  23. while pdict:
  24. time.sleep(10)
  25. for pname in pdict:
  26. if pdict[pname].exitcode == None or pdict[pname].is_alive():
  27. pdict[pname].terminate() #killing old; that should also free file resource
  28. pdict[pname] = Process(target = write_file, args = (pname,))
  29. pdict[pname].start() #simply creating new and starting
  30. else:
  31. del pdict[pname]
Add Comment
Please, Sign In to add comment