-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd1make-server.py
executable file
·79 lines (66 loc) · 2.27 KB
/
d1make-server.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
#!/usr/bin/env python
import os
import subprocess
import sys
import threading
import time
from FIFOServerThread import FIFOServerThread
from CallDispatcher import CallDispatcher
class ExecuteThread(threading.Thread):
def __init__(self,
stdout_fifo, stderr_fifo, exit_code_fifo,
directory, command):
threading.Thread.__init__(self)
self.stdout_fifo = stdout_fifo
self.stderr_fifo = stderr_fifo
self.exit_code_fifo = exit_code_fifo
self.directory = directory
self.command = command
def run(self):
stdout_fd = os.open(self.stdout_fifo, os.O_WRONLY)
stderr_fd = os.open(self.stderr_fifo, os.O_WRONLY)
exit_code_fd = os.open(self.exit_code_fifo, os.O_WRONLY)
p = subprocess.Popen("cd " + self.directory + " && " + self.command,
shell=True,
stdout=stdout_fd,
stderr=stderr_fd)
p.communicate()
exit_code = p.wait()
os.close(stdout_fd)
os.close(stderr_fd)
os.write(exit_code_fd, str(exit_code) + "\n")
os.close(exit_code_fd)
class ServerMakeStarter(CallDispatcher, FIFOServerThread):
def __init__(self):
FIFOServerThread.__init__(self)
self.running_threads = set()
def call_compile(self,
stdout_fifo, stderr_fifo, exit_code_fifo,
directory, command):
t = ExecuteThread(stdout_fifo, stderr_fifo, exit_code_fifo,
directory, command)
t.start()
self.running_threads.add(t)
def purge_finished(self):
ts = self.running_threads
self.running_threads = set()
for t in ts:
if t.isAlive():
self.running_threads.add(t)
def threads_left(self):
self.purge_finished()
return len(self.running_threads)
def main():
sms = ServerMakeStarter()
sms.start()
try:
while True:
left = sms.threads_left()
load = os.getloadavg()
print sms.fifo, "RUNNING", left, " ".join((str(l) for l in load))
sys.stdout.flush()
time.sleep(min(10 + load[0], 60))
finally:
sms.stop()
if __name__ == "__main__":
main()