2013-06-11 21:55:17 +02:00
|
|
|
import threading
|
2021-06-03 15:20:39 +02:00
|
|
|
|
2020-01-16 15:32:51 +01:00
|
|
|
from . import pypofetch
|
2013-06-11 23:26:48 +02:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
def __timeout(func, timeout_duration, default, args, kwargs):
|
2013-06-11 21:55:17 +02:00
|
|
|
class InterruptableThread(threading.Thread):
|
|
|
|
def __init__(self):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.result = default
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
def run(self):
|
|
|
|
self.result = func(*args, **kwargs)
|
|
|
|
|
|
|
|
first_attempt = True
|
|
|
|
|
|
|
|
while True:
|
|
|
|
it = InterruptableThread()
|
|
|
|
it.start()
|
2013-10-07 21:39:14 +02:00
|
|
|
if not first_attempt:
|
|
|
|
timeout_duration = timeout_duration * 2
|
2013-06-11 21:55:17 +02:00
|
|
|
it.join(timeout_duration)
|
|
|
|
|
2021-08-15 18:47:24 +02:00
|
|
|
if it.is_alive():
|
2021-05-27 16:23:02 +02:00
|
|
|
"""Restart Liquidsoap and try the command one more time. If it
|
2013-06-11 21:55:17 +02:00
|
|
|
fails again then there is something critically wrong..."""
|
|
|
|
if first_attempt:
|
2021-05-27 16:23:02 +02:00
|
|
|
# restart liquidsoap
|
2013-06-11 23:26:48 +02:00
|
|
|
pypofetch.PypoFetch.ref.restart_liquidsoap()
|
2013-06-11 21:55:17 +02:00
|
|
|
else:
|
|
|
|
raise Exception("Thread did not terminate")
|
|
|
|
else:
|
|
|
|
return it.result
|
|
|
|
|
|
|
|
first_attempt = False
|
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2013-10-07 21:37:34 +02:00
|
|
|
def ls_timeout(f, timeout=15, default=None):
|
2013-06-11 21:55:17 +02:00
|
|
|
def new_f(*args, **kwargs):
|
|
|
|
return __timeout(f, timeout, default, args, kwargs)
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
return new_f
|