|
a |
|
b/Concurrent.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "markdown", |
|
|
5 |
"metadata": {}, |
|
|
6 |
"source": [ |
|
|
7 |
"# Concurrent.Futures.ThreadPoolExecutor" |
|
|
8 |
] |
|
|
9 |
}, |
|
|
10 |
{ |
|
|
11 |
"cell_type": "code", |
|
|
12 |
"execution_count": 14, |
|
|
13 |
"metadata": {}, |
|
|
14 |
"outputs": [], |
|
|
15 |
"source": [ |
|
|
16 |
"from concurrent.futures import ThreadPoolExecutor #using parallel threads \n", |
|
|
17 |
"import time #time module" |
|
|
18 |
] |
|
|
19 |
}, |
|
|
20 |
{ |
|
|
21 |
"cell_type": "code", |
|
|
22 |
"execution_count": 6, |
|
|
23 |
"metadata": {}, |
|
|
24 |
"outputs": [], |
|
|
25 |
"source": [ |
|
|
26 |
"def task(n):\n", |
|
|
27 |
" print(\"Processing {}\".format(n))\n", |
|
|
28 |
" time.sleep(10) #time.sleep pause the program. So it can be used to chech working of multiple thread" |
|
|
29 |
] |
|
|
30 |
}, |
|
|
31 |
{ |
|
|
32 |
"cell_type": "code", |
|
|
33 |
"execution_count": 11, |
|
|
34 |
"metadata": {}, |
|
|
35 |
"outputs": [], |
|
|
36 |
"source": [ |
|
|
37 |
"def main():\n", |
|
|
38 |
" print(\"Starting ThreadPoolExecutor\")\n", |
|
|
39 |
" with ThreadPoolExecutor(max_workers=3) as executor:\n", |
|
|
40 |
" future = executor.submit(task, (2)) #working of multiple threads\n", |
|
|
41 |
" future = executor.submit(task, (3))\n", |
|
|
42 |
" future = executor.submit(task, (4))\n", |
|
|
43 |
" print(\"All tasks complete\")" |
|
|
44 |
] |
|
|
45 |
}, |
|
|
46 |
{ |
|
|
47 |
"cell_type": "code", |
|
|
48 |
"execution_count": 13, |
|
|
49 |
"metadata": {}, |
|
|
50 |
"outputs": [ |
|
|
51 |
{ |
|
|
52 |
"name": "stdout", |
|
|
53 |
"output_type": "stream", |
|
|
54 |
"text": [ |
|
|
55 |
"Starting ThreadPoolExecutor\n", |
|
|
56 |
"Processing 2\n", |
|
|
57 |
"Processing 3\n", |
|
|
58 |
"Processing 4\n", |
|
|
59 |
"All tasks complete\n" |
|
|
60 |
] |
|
|
61 |
} |
|
|
62 |
], |
|
|
63 |
"source": [ |
|
|
64 |
"if __name__ == '__main__':\n", |
|
|
65 |
" main() #main function" |
|
|
66 |
] |
|
|
67 |
} |
|
|
68 |
], |
|
|
69 |
"metadata": { |
|
|
70 |
"kernelspec": { |
|
|
71 |
"display_name": "Python 2", |
|
|
72 |
"language": "python", |
|
|
73 |
"name": "python2" |
|
|
74 |
}, |
|
|
75 |
"language_info": { |
|
|
76 |
"codemirror_mode": { |
|
|
77 |
"name": "ipython", |
|
|
78 |
"version": 2 |
|
|
79 |
}, |
|
|
80 |
"file_extension": ".py", |
|
|
81 |
"mimetype": "text/x-python", |
|
|
82 |
"name": "python", |
|
|
83 |
"nbconvert_exporter": "python", |
|
|
84 |
"pygments_lexer": "ipython2", |
|
|
85 |
"version": "2.7.15" |
|
|
86 |
} |
|
|
87 |
}, |
|
|
88 |
"nbformat": 4, |
|
|
89 |
"nbformat_minor": 2 |
|
|
90 |
} |