-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
48 lines (42 loc) · 1.46 KB
/
run.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
import subprocess
import threading
import os
import sys
import fcntl
import select
from colorama import init
from termcolor import colored
def set_nonblocking(fd):
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
def run_command(command, app_name, color):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
set_nonblocking(process.stdout.fileno())
set_nonblocking(process.stderr.fileno())
while True:
reads, _, _ = select.select([process.stdout, process.stderr], [], [])
for fd in reads:
output = fd.readline().decode().strip()
if output:
print(colored(f"[{app_name}]", color), output)
if process.poll() is not None:
break
return process.returncode
def run_webserver():
app_name = "webserver"
command = "cd webserver && go run main.go"
return run_command(command, app_name, "green")
def run_viz():
app_name = "viz"
command = "cd viz && npm run start"
return run_command(command, app_name, "magenta")
def run_mlp():
app_name = "mlp"
command = "cd mlp && python api.py"
return run_command(command, app_name, "blue")
# Runs each service as a seperate thread
thread_names = [run_webserver, run_viz, run_mlp]
threads = [threading.Thread(target=thread) for thread in thread_names]
init()
[thread.start() for thread in threads]
[thread.join() for thread in threads]