-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_sess_mp.py
60 lines (50 loc) · 1.39 KB
/
tf_sess_mp.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
import tensorflow as tf
import multiprocessing as mp
import time
import threading
class Worker():
def __init__(self):
print("Worker_init")
def sess_test1(self):
print("sess 1 is running")
z = tf.Variable(1, name='PID1_Z')
a = tf.get_variable('var_a', 2)
def sess_test2(self):
print("sess 2 is running")
with tf.name_scope('aaa'):
c = tf.Variable(3, name='var_c')
def sess_test3(self):
print("sess 3 is running")
with tf.variable_scope('bbb'):
d = tf.Variable(4, name='var_d')
def event_test(self,e):
print("sleep 3 s")
time.sleep(3)
e.set()
#主进程中定义global全局变量,无法与子进程共享
global s
a = Worker()
print("s is ",s)
#e = multiprocessing.Event()
#print('the event_set is:',e.is_set())
with mp.Manager() as manager:
p = mp.Process(target=a.sess_test1,args=())
q = mp.Process(target=a.sess_test2,args=())
r = mp.Process(target=a.sess_test3,args=())
p.start()
q.start()
r.start()
p.join()
q.join()
r.join()
print([str(i.name) for i in tf.global_variables()])
print([str(i.name) for i in tf.local_variables()])
'''
for j in range(10):
pool = multiprocessing.Pool(processes=4)
#pool.apply_async(a.act, (d,i))
pool.apply_async(a.sess_test1, (e, 2))
pool.apply_async(a.event_test, (e, ))
pool.close()
pool.join()
'''