fork download
  1. # %%
  2. from multiprocessing import Pool, Process
  3. import hashlib
  4. from random import random
  5. from time import time
  6. import concurrent.futures
  7.  
  8.  
  9. start_time = time()
  10. workers = 1
  11. def compute(z):
  12. id = z
  13. print('Start id =', id)
  14. for cnt in range(10):
  15. ar = [hashlib.md5(b'%s' % bytes(i)) for i in range(30000)]
  16. print(cnt, 'id =', id)
  17. print('Finish id =', id)
  18.  
  19.  
  20.  
  21. ids = [random(), random(), random(),random(), random(), random(),random(), random(), random(),]
  22.  
  23. # %%
  24.  
  25. # We can use a with statement to ensure threads are cleaned up promptly
  26. with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor:
  27. # Start the load operations and mark each future with its URL
  28. future_to_url = {executor.submit(compute, url): url for url in ids}
  29. print('after future')
  30. for future in concurrent.futures.as_completed(future_to_url):
  31. url = future_to_url[future]
  32. try:
  33. data = future.result()
  34. except Exception as exc:
  35. print('Exception' % (url, exc))
  36. else:
  37. print('Finished', url)
  38.  
  39.  
  40. print('All done. Time taken ', round(time() - start_time, 3), ', Workers ', workers)
  41.  
  42. '''
  43. # Python 3.6 buggy
  44. All done. Time taken 45.307 , Workers 1
  45. All done. Time taken 32.062 , Workers 2
  46. All done. Time taken 27.553 , Workers 3
  47. All done. Time taken 38.718 , Workers 4
  48. All done. Time taken 40.013 , Workers 4
  49. All done. Time taken 47.477 , Workers 5
  50. All done. Time taken 45.283 , Workers 9
  51. All done. Time taken 45.413 , Workers 12
  52. All done. Time taken 43.496 , Workers 12
  53. All done. Time taken 45.294 , Workers 12
  54. All done. Time taken 45.059 , Workers None
  55.  
  56. # Python 3.8
  57. All done. Time taken 48.34 , Workers 12
  58. All done. Time taken 42.096 , Workers 4
  59. All done. Time taken 40.636 , Workers 4
  60. All done. Time taken 30.949 , Workers 3
  61. All done. Time taken 31.034 , Workers 2
  62. All done. Time taken 48.932 , Workers 1
  63.  
  64. '''
  65. # %%
  66.  
Time limit exceeded #stdin #stdout 5s 27168KB
stdin
Standard input is empty
stdout
Standard output is empty