-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_thread_pool.py
33 lines (28 loc) · 939 Bytes
/
7_thread_pool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import cProfile
import pstats
import threading
from multiprocessing.dummy import Pool as ThreadPool
import time
import random
import numpy as np
def slow_function(n):
time.sleep(n)
def run_slow_functions_80x_parallel():
sleep_ns = [random.random()*3 for x in range(80)]
mean_sleep = np.mean(sleep_ns)
print('Mean sleeping time per function call = %f' % mean_sleep)
print('Total sleeping time = %f' % (mean_sleep*80))
print('Ideal world parallel time = %f' % (mean_sleep*10))
pool = ThreadPool(8)
pool.map(slow_function, sleep_ns)
pool.close()
pool.join()
def main():
print('==== Running 80x slow function in parallel ====')
cProfile.run('run_slow_functions_80x_parallel()', filename='parallel')
print('\n===== Parallel statistics: ====')
py_stats2 = pstats.Stats('parallel')
py_stats2.print_stats('parallel')
print('All done!')
if __name__ == "__main__":
main()