-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript.py
97 lines (79 loc) · 2.48 KB
/
script.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from main import train
import torch
import subprocess
import gc
import sys
import psutil
from util.memory_gpu_usage import get_gpu_memory_usage
#import multiprocessing
#from multiprocessing import Pool
#from multiprocessing import Pool, TimeoutError
#import contextlib
#import multiprocessing
# We must import this explicitly, it is not imported by the top-level
# multiprocessing module.
import multiprocessing.pool
import numpy as np
import time
import os
from option import lst_parameters_change
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="0"
'''
def daemon(index):
print 'Starting:', multiprocessing.current_process().name
#time.sleep(2)
print('Index %d' % index)
#train(index)
print 'Exiting :', multiprocessing.current_process().name
def non_daemon(index):
print 'Starting:', multiprocessing.current_process().name
print('Index %d' % index)
train(index)
print 'Exiting :', multiprocessing.current_process().name
if __name__ == '__main__':
n = multiprocessing.Process(name='non-daemon', target=non_daemon, args=(2,))
n.daemon = False
n.start()
n.join()
print('... Finished')
'''
class NoDaemonProcess(multiprocessing.Process):
# make 'daemon' attribute always return False
def _get_daemon(self):
return False
def _set_daemon(self, value):
pass
daemon = property(_get_daemon, _set_daemon)
# We sub-class multiprocessing.pool.Pool instead of multiprocessing.Pool
# because the latter is only a wrapper function, not a proper class.
class NoDaemonPool(multiprocessing.pool.Pool):
Process = NoDaemonProcess
def work(num_proc):
print("Creating %i (daemon) worker." % num_proc)
try:
train(index=num_proc)
except:
print('Exception in worker: %d',num_proc)
return False
return True
def script():
max_pool_tasks = 3
print("Creating 5 (non-daemon) workers and jobs in main process.")
max_tasks = len(lst_parameters_change)
if max_pool_tasks > max_tasks:
max_pool_tasks = max_tasks
for i in np.arange(0,10,max_pool_tasks):
range_ini_task = i
range_end_task = i + max_pool_tasks
if range_end_task >= max_tasks:
range_end_task = max_tasks
pool = NoDaemonPool(max_pool_tasks)
pool.map(work, range(range_ini_task,range_end_task))
pool.close()
pool.join()
# Finish
if range_end_task >= max_tasks:
break
if __name__ == "__main__":
script()