-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_controller.py
More file actions
36 lines (31 loc) · 899 Bytes
/
server_controller.py
File metadata and controls
36 lines (31 loc) · 899 Bytes
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
import subprocess
import shlex
import threading
class BDS_Wrapper(subprocess.Popen):
def __init__(self, exec_path, **kwargs):
super().__init__(
exec_path,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
creationflags = subprocess.CREATE_NO_WINDOW,
**kwargs
)
def read_output(self, output_handler):
# Spawns a new thread to read the output.
# The thread calls output_handler with the string that was read.
# Returns the thread.
def worker():
for line in iter(self.stdout.readline, b''):
output_handler(line.decode("utf-8"))
return threading.Thread(target=worker)
def is_running(self):
return self.poll() == None
def write(self, command_string, terminator = "\n"):
if self.is_running():
data = command_string + terminator
self.stdin.write(data.encode())
self.stdin.flush()
return True
else:
return False