-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun_local.py
104 lines (81 loc) · 2.71 KB
/
run_local.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
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
# -*- coding: utf-8 -*-
"""
script to run a local, self contained server
python 3.6 or higher required
currently is a candidate to use asyncio
"""
import logging
import time
import platform
from subprocess import Popen, call
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('tailor.launcher')
processes = (
('service_cmd', 'apps/service/__main__.py'),
('kiosk_server_cmd', 'apps/server/__main__.py'),
('kiosk_cmd', 'apps/kiosk/__main__.py'))
# TODO: find python cmd name on whatever platform
system = platform.system()
if system == 'Linux':
python_cmd = '/usr/bin/python3'
elif system == 'Windows':
python_cmd = "C:\\Users\\Leif\\AppData\\Local\\Programs\\Python\\Python36\\python.exe"
# TODO: use subprocess.run
def run_processes():
for name, cmd in processes:
logger.debug('starting process %s', name)
args = [python_cmd, cmd]
proc = Popen(args)
time.sleep(2)
yield proc
if __name__ == '__main__':
# TODO: check for running in gnome environment
# TODO: release_gvfs_from_camera fails in windows. provide better check in future
if system == 'Linux':
from tailor.platform.unix import release_gvfs_from_camera
try:
release_gvfs_from_camera()
except FileNotFoundError:
pass
running_processes = list()
try:
for proc in run_processes():
running_processes.append(proc)
running = True
while running:
for proc in running_processes:
value = proc.poll()
if value is not None:
logger.debug('one process has quit')
running = False
break
time.sleep(.1)
except:
import traceback
traceback.print_last()
logger.debug('an exception was raised and program will now terminate')
finally:
logger.debug('cleaning up...')
for proc in running_processes:
try:
start = time.time()
while proc.poll() is None:
if time.time() - start > 10:
break
try:
proc.terminate()
proc.kill()
except EnvironmentError:
pass
# TODO: find the correct exception to catch
except:
pass
# werkzerg/flask refuses to close using subprocess
# so here is my heavy handed hack until i get it
# figured out
# TODO: better process cleanup
if system == 'Linux':
try:
call(['killall', '-KILL', 'python3'], timeout=10)
except FileNotFoundError:
pass