shrooms-server/utils.py

51 lines
1.6 KiB
Python

import subprocess
import threading
import os
import json
import queue
process = None
update_thread = None
update_queue = queue.LifoQueue(maxsize=1)
def update_loop(p, q):
print("update_loop start")
while p.poll() is None:
msg = q.get()
#print("got msg")
p.stdin.write(bytes(json.dumps(msg) + "\n", "utf8"))
p.stdin.flush()
def start_process():
print("starting shroom pipe")
global process, update_thread
if process is not None and process.poll() is None:
print("shroom pipe is still running, exiting init")
return
try:
is_mock = os.environ['MOCK']
except KeyError:
is_mock = False
if is_mock:
process = subprocess.Popen(["/usr/bin/env", "python", "/home/kelvin/src/shroom-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
else:
#process = subprocess.Popen(["ssh", "shrooms@threefortiethofonehamster.com", "/usr/bin/env", "python3", "/home/shrooms/shrooms-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process = subprocess.Popen(["ssh", "shrooms@35.211.7.97", "/usr/bin/env", "python3", "/home/shrooms/shrooms-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
update_thread = threading.Thread(target=update_loop, args = (process, update_queue))
update_thread.start()
def running():
global process
return process is not None and process.poll() is None
def send_update(msg):
global update_queue
try:
update_queue.put_nowait(msg)
except queue.Full:
print("queue full, skipping message")