This is honestly so cool....
import multiprocessing
import time
def mp_worker(filename):
time.sleep(1)
return filename * filename
def mp_handler():
p = multiprocessing.Pool(32)
filenames = [1, 2, 3, 4, 5, 6] * 32
start = time.time()
with open('results.txt', 'w') as f:
for result in p.imap(mp_worker, filenames):
f.write(f'{result}\n')
end = time.time()
print(f'Multiple Threads: {round(end - start, 2)} seconds')
f.close()
if __name__=='__main__':
mp_handler()