-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_multiworker.py
More file actions
64 lines (48 loc) · 1.49 KB
/
test_multiworker.py
File metadata and controls
64 lines (48 loc) · 1.49 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
from multiprocessing import Process, Queue, Pool
import os
import argparse
from worker import CustomWorker
# from worker import train_model
import dna
import models
class Scheduler:
def __init__(self, workerids):
self._queue = Queue()
self.workerids = workerids
self._results = Queue()
self.__init_workers()
def __init_workers(self):
self._workers = list()
for wid in self.workerids:
self._workers.append(CustomWorker(wid, self._queue, self._results))
def start(self, xlist):
# put all of models into queue
for model_info in xlist:
self._queue.put(model_info)
#add a None into queue to indicate the end of task
self._queue.put(None)
#start the workers
for worker in self._workers:
worker.start()
# wait all fo workers finish
for worker in self._workers:
worker.join()
print "all of workers have been done"
for i in range(len(xlist)):
print(i)
a = self._results.get()
print(a)
def run(workerids):
#scan all files under img_path
xlist = list()
for i in range(5):
xlist.append(dna.random_net(str(i), 28 * 28, 10, 4))
#init scheduler
x = Scheduler(workerids)
#start processing and wait for complete
x.start(xlist)
if __name__ == "__main__":
# args = parser.parse_args()
# workerids = '0, 1'
workerids = [0, 1]
run(workerids)