-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch-subs-runner.py
executable file
·57 lines (50 loc) · 1.52 KB
/
fetch-subs-runner.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
#! /usr/bin/env python3
# workaround for: asyncio hangs
# python -u fetch-subs-runner.py 2>&1 | tee -a fetch-subs-runner.py.log
# t_sleep = random.randrange(20, 60)
dt_max = 60 + 10
import sys
import subprocess
import time
import contextlib
import signal
# https://stackoverflow.com/questions/15018519/python-timeout-context-manager-with-threads
class TimeoutException(Exception):
pass
def timeout_handler(signum, frame):
raise TimeoutException()
@contextlib.contextmanager
def timeout(seconds):
old_handler = signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, old_handler)
while True:
args = [
sys.executable, # python exe
"fetch-subs.py",
]
proc = subprocess.Popen(
args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf8",
)
# https://stackoverflow.com/questions/18421757/live-output-from-subprocess-command
# replace "" with b"" for Python 3
lines = iter(proc.stdout.readline, "")
while True:
try:
with timeout(dt_max):
line = next(lines)
except TimeoutException:
print("fetch-subs-runner.py: TimeoutException -> restarting fetch-subs.py")
break
except StopIteration:
print("fetch-subs-runner.py: StopIteration -> restarting fetch-subs.py in 30 seconds")
time.sleep(30)
break
sys.stdout.write(line)