-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
128 lines (77 loc) · 2.64 KB
/
Copy pathmain.py
File metadata and controls
128 lines (77 loc) · 2.64 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/local/bin/python
import datetime
import os
import time
from multiprocessing import Queue
from Queue import Empty
import signal
import sys
import threading
class Child (threading.Thread):
def __init__(self, chld_name, queue, e):
threading.Thread.__init__(self)
self.queue = queue
self.chld_name = chld_name
self.event = e
def child_signal_handler(self):
#self.event.set()
if self.event.is_set():
pid = os.getpid()
isClosed = os.kill(pid, 0)
if isClosed == None:
print "Child %s with pid: %d closed!" % (self.chld_name, pid)
self.event.clear()
sys.exit(0)
else:
print "Child still working..."
def run(self):
#
print "Starting " + self.name
while not self.event.is_set():
# self.child_signal_handler()
#while self.queue:
try:
q = self.queue.get()
print '%s: have: %s' % (self.chld_name, q)
except Empty, e:
print '%s: Empty' % (self.chld_name)
time.sleep(2)
#self.event.wait()
class Parent(object):
def __init__(self):
self.q = Queue()
self.event = threading.Event()
self.child_1 = Child("Child-1", self.q, self.event)
self.child_2 = Child("Child-2", self.q, self.event)
signal.signal(signal.SIGTERM, self.signal_term_handler)
self.cur_pid = os.getpid()
def writeToQueue(self):
while True:
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
self.q.put(st)
time.sleep(1)
def TerminateAll(self):
if self.termProc == True:
#for child in self.childlist:
print "Terminate child: %s" % (self.child_1.name)
#self.child_1.quit()
print "Terminate child: %s" % (self.child_2.name)
#self.child_2.quit()
sys.exit(0)
#os.kill(l)
else:
pass
def signal_term_handler(self, signal, frame):
self.event.set()
self.termProc = True
print 'got SIGTERM, Event started'
self.TerminateAll()
def CreateChild(self):
print "Parent pid is: %s" % (self.cur_pid)
self.child_1.start()
self.child_2.start()
pr = Parent()
pr.CreateChild()
pr.writeToQueue()
#pr.writeToQueue()