-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample2.py
More file actions
79 lines (61 loc) · 2.33 KB
/
example2.py
File metadata and controls
79 lines (61 loc) · 2.33 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
# encoding: utf-8
# for python 2.7
from __future__ import print_function
from __future__ import unicode_literals
from builtins import range
from progressbarsimple import ProgressBar
import multiprocessing, time
def foo(uselessInput):
"""
Function foo running a silly for loop to waste some time
Args:
uselessInput (any): not used input, just needed for the pool map function
Returns:
minutesSolve: minutes needed for iterating through the for-loop
secsSolve: seconds needed for iterating through the for-loop
"""
timeStart = time.clock()
for i in range(1000):
i + 100 * i
time.sleep(0.2)
timeSolverSolve = time.clock() - timeStart
minutesSolve = int(timeSolverSolve / 60.)
secsSolve = timeSolverSolve - minutesSolve * 60.
return minutesSolve, secsSolve
# start timing of the hole process
timeStartBatch = time.time()
# define a batch data list iterator
batchDataList = range(100)
numberWorkers = multiprocessing.cpu_count()
# create a multiprocess iterator named "results"
pool = multiprocessing.Pool(numberWorkers)
results = pool.imap_unordered(foo, batchDataList)
pool.close()
# create a loading bar
nElements = 50
nSteps = len(batchDataList)
progressBar = ProgressBar(nElements, nSteps, suppressPrint=False)
print('=====================================')
print('------Multiprocessing Batch Job------')
print('numberWorkers: {}'.format(numberWorkers))
print('numberOfEval.: {}'.format(len(batchDataList)))
# run the batch jobs
completed = 0
while completed <= nSteps:
progressBar.progressMultiprocessing(results._index)
if completed < results._index:
completed = results._index
pool.join()
# print the results of all the batch simulations
print('=====================================')
for batchJobIndex, [minutesSolve, secsSolve] in enumerate(results):
print('____________Batch {:5} ___________'.format(batchJobIndex + 1))
print('Runtime: {} min {} sec'.format(minutesSolve, secsSolve))
print('=====================================')
# print the time needed for the hole process
timeBatchJob = time.time() - timeStartBatch
minutesBatch = int(timeBatchJob / 60.)
secsBatch = timeBatchJob - minutesBatch * 60.
print('total runtime: {} min {} sec'.format(minutesBatch, secsBatch))
print('=====================================')