Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from multiprocessing import Pool
- def write_file(file: str):
- f = open(file, 'w')
- for item in range(0, 1500000):
- f.write("%sn" % item)
- f.close()
- if __name__ == '__main__':
- list_files = ['1.txt', '2.txt', '3.txt']
- with Pool(3) as p:
- p.map(write_file, list_files)
- from multiprocessing import Process
- import time
- pdict = {}
- for fname in list_files:
- p = Process(target = write_file, args = (fname,))
- pdict[fname] = p
- p.start()
- while pdict:
- time.sleep(10)
- for pname in pdict:
- if pdict[pname].exitcode == None or pdict[pname].is_alive():
- pdict[pname].terminate() #killing old; that should also free file resource
- pdict[pname] = Process(target = write_file, args = (pname,))
- pdict[pname].start() #simply creating new and starting
- else:
- del pdict[pname]
Add Comment
Please, Sign In to add comment