本文整理汇总了Python中repeated_timer.RepeatedTimer.run方法的典型用法代码示例。如果您正苦于以下问题:Python RepeatedTimer.run方法的具体用法?Python RepeatedTimer.run怎么用?Python RepeatedTimer.run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类repeated_timer.RepeatedTimer
的用法示例。
在下文中一共展示了RepeatedTimer.run方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from repeated_timer import RepeatedTimer [as 别名]
# 或者: from repeated_timer.RepeatedTimer import run [as 别名]
class CommandRunner:
UPDATE_INTERVAL = 5
def __init__(self, msg_func, master):
self.msg_func = msg_func
self.master = master
self.child_process = None
self.output_queue = queue.Queue()
self.output_read_thread = None
self.output_mutex = threading.RLock()
self.update_timer = RepeatedTimer(self.on_update_timer)
self._exitcode = None
def _read_output(self):
while True:
# NOTE: don't use iterator interface since it uses an
# internal buffer and we don't see output in a timely fashion
line = self.child_process.stdout.readline()
if not line:
break
self.output_queue.put(line)
def is_alive(self):
if self.child_process is None:
return False
return self.child_process.poll() is None
def run_command(self, *args):
if self.is_alive():
raise ValueError("already running a command")
cmd = [sys.executable, "-u", "-m", "openslides"]
cmd.extend(args)
creationflags = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0)
self._exitcode = None
self.child_process = subprocess.Popen(
cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, creationflags=creationflags)
self.child_process.stdin.close()
self.output_read_thread = threading.Thread(target=self._read_output)
self.output_read_thread.start()
self.update_timer.run(self.UPDATE_INTERVAL)
self.master.event_generate("<<EVT_CMD_START>>", when="tail")
def cancel_command(self):
if not self.is_alive():
return
self.msg_func("Stopping server...\n")
# hard-kill the spawned process tree
# TODO: offer the server the chance for a clean shutdown
proc = psutil.Process(self.child_process.pid)
kill_procs = [proc]
kill_procs.extend(proc.children(recursive=True))
for p in kill_procs:
p.kill()
def on_update_timer(self):
is_alive = self.is_alive()
if not is_alive:
# join thread to make sure everything was read
self.output_read_thread.join(5)
if self.output_read_thread.is_alive():
self.msg_func("Internal error: failed to join output reader thread")
self.output_read_thread = None
for line_no in itertools.count():
try:
data = self.output_queue.get(block=False)
except queue.Empty:
break
else:
# XXX: check whether django uses utf-8 or locale for
# it's cli output
text = data.decode("utf-8", errors="replace")
with self.output_mutex:
self.msg_func(text)
# avoid waiting too long here if child is still alive
if is_alive and line_no > 10:
break
if not is_alive:
self._exitcode = self.child_process.returncode
self.update_timer.stop()
self.child_process = None
self.master.event_generate("<<EVT_CMD_STOP>>", when="tail")
def append_message(self, text, newline="\n"):
with self.output_mutex:
self.msg_func(text + newline)
@property
def exitcode(self):
#.........这里部分代码省略.........