简体   繁体   English

Python暂停线程,手动执行并重置时间

[英]Python pause thread, do manually and reset time

I need to call function every x seconds but with option to call it manually and in this case reset time. 我需要每隔x秒调用一次函数,但可以选择手动调用该函数,在这种情况下需要重置时间。 I have sth like this: 我有这样的事情:

import time
import threading

def printer():
    print("do it in thread")

def do_sth(event):
    while not event.is_set():
        printer()
        time.sleep(10)

event = threading.Event()
print("t - terminate, m - manual")
t = threading.Thread(target=do_sth, args=(event,))
t.daemon = True
t.start()
a = input()
if a == 't':
    event.set()
elif a == 'm':
    event.wait()
    printer()
    event.clear()

UPDATE: I have found something that helped me a lot: Python - Thread that I can pause and resume Now my code look like this: 更新:我发现了一些对我有很大帮助的东西: Python-我可以暂停和恢复的线程现在我的代码如下:

import threading, time, sys

class ThreadClass(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.can_run = threading.Event()
        self.thing_done = threading.Event()
        self.thing_done.set()
        self.can_run.set()

    def run(self):
        while True:
            self.can_run.wait()
            try:
                self.thing_done.clear()
                self.do_in_thread()
            finally:
                self.thing_done.set()
            time.sleep(5)

    def pause(self):
        self.can_run.clear()
        self.thing_done.wait()

    def resume(self):
        self.can_run.set()

    def do_in_thread(self):
        print("Thread...1")
        time.sleep(2)
        print("Thread...2")
        time.sleep(2)
        print("Thread...3")


def do_in_main():
    print("Main...1")
    time.sleep(2)
    print("Main...2")
    time.sleep(2)
    print("Main...3")

if __name__ == '__main__':

    t = ThreadClass()
    t.daemon = True
    t.start()
    while True:
        i = input()
        if i == 'm':
            t.pause()
            do_in_main()
            t.resume()
        elif i == 't':
            sys.exit()
            # t.join()

The only problem is that when I terminate, a would like thread to finish its job before it exits. 唯一的问题是,当我终止时,一个希望线程在退出之前完成其工作。

It may be that buffered outputs are the culprits and thus - you're are getting your expected behaviour. 缓冲的输出可能是罪魁祸首,因此-您正在得到预期的行为。

I changed your code to the following, and it seems to do something (if that's what you wanted, is up to you): 我将您的代码更改为以下代码,它似乎可以执行某些操作(如果这正是您想要的,则取决于您):

import time
import threading

def printer():
    print("do it in thread")

def do_sth(event):
    print("event.is_set:", event.is_set())
    while not event.is_set():
        printer()
        time.sleep(10)

event = threading.Event()
print("t - terminate, m - manual")
t = threading.Thread(target=do_sth, args=(event,))
print("t:",t)
t.daemon = True
t.start()
a = input()
if a == 't':
    event.set()
elif a == 'm':
    event.wait()
    printer()
    event.clear()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM