简体   繁体   English

如何随意停止和启动线程

[英]How to stop and start a thread at will

So I'm doing some testing with threads and I realised I could not stop and then start a thread. 因此,我正在对线程进行一些测试,我意识到无法停止然后再启动线程。 I could stop it, but starting it again was the issue. 我可以停止它,但是再次开始是问题。

I want a script that adds 1 to a var when it is on then its stops when off by pressing shift to turn on and off. 我想要一个脚本,该脚本在打开时将1加到var,然后在关闭时通过按shift打开和关闭它而停止。
I have the detecting shift working (it is on another part of my code), but I just need to find out how to stop and start threads 我正在检测轮班工作(在代码的另一部分中),但是我只需要了解如何停止和启动线程

Here is my test code: 这是我的测试代码:

from threading import Thread as th
import time as t
var = 0
def testDef():
    global var
    var += 1:
    t.sleep(1)
test = th(target = testDef)
test.start()
while True:
    menu = input("On, Off, Show Var")
    if menu == "On":
        test.start()
    elif menu == "Off":
        test._stop():
    elif menu == "S":
        print(var)

I know there are a few errors, but I mainly need the on and off threading to work. 我知道有一些错误,但是我主要需要打开和关闭线程才能工作。 Thanks, Jeff. 谢谢,杰夫。

As far as I know, you can't actually stop and restart a thread as you can't use test.start() when the method has been terminated. 据我所知,当方法终止时,您实际上不能停止并重新启动线程,因为您不能使用test.start() However, you may be wondering to something similar by using threading.Condition to pause and later resume the execution. 但是,您可能想知道通过使用threading.Condition来暂停并稍后恢复执行的类似操作。

You can read more about it in the documentation . 您可以在文档中阅读有关它的更多信息。

There is also an error in var += 1: , change it to var += 1 var += 1:也存在错误,请将其更改为var += 1

Here's a simple example on how to use threading.Event to enable two threads to communicate. 这是一个有关如何使用threading.Event来使两个线程进行通信的简单示例。 This works by setting the internal flag of the Event to either True or False. 通过将事件的内部标志设置为True或False,可以工作。 While this internal flag is False you can ask thread a to wait (effectively block, which is not very efficient by the way). 当此内部标志为False您可以要求线程a wait (有效地阻塞,这并不是很有效)。 Then we use the two timers (b, c) to simulate a shift press every 5 seconds. 然后,我们使用两个计时器(b, c)每隔5秒模拟一次换档。 In order to release a we set the event (internal flag = True). 为了释放a事件,我们set了事件(内部标志= True)。 5 seconds later, we clear the value of the internal flag and this will make thread a to block again. 5秒后,我们clear内部标志的值,这将使线程a再次阻塞。

import threading

def do(event):
    flag = True
    while flag:
        if not event.isSet():
            print "blocking"
            event.wait()
        else:
            print "resuming"


def pressShift(event, enable):
    print "Shift pressed"
    if enable:
        event.set()
    else:
        event.clear()

def main():
    event = threading.Event()
    a = threading.Thread(target=do, args=(event,))
    b = threading.Timer(5, pressShift, args=(event, True)).start()
    c = threading.Timer(10, pressShift, args=(event, False)).start()
    a.start()
    a.join()

if __name__ == "__main__":
    main()

You cannot restart a thread that has already been started. 您无法重新启动已经启动的线程。 What you can do, however, is to create another thread. 但是,您可以做的是创建另一个线程。

from threading import Thread as th
import time as t
var = 0
def testDef():
    global var
    var += 1
    t.sleep(1)
test = th(target = testDef)
test.start()
while True:
    menu = input("On, Off, Show Var")
    if menu == "On":
        test = th(target = testDef)
        test.start()
    elif menu == "Off":
        test._stop()
    elif menu == "S":
        print(var)

Use an event object like this post , and check that event in your target functoin. 使用类似于此post事件对象 ,并在目标functoin中检查该事件。 Also, you need a new thread each time you re-start . 另外, 每次重新启动时需要一个新线程 The code shown below adds some debugging that should be useful. 下面显示的代码添加了一些有用的调试信息。 (Another approach is to build a custom stop function .) (另一种方法是构建自定义停止功能 。)

import logging
import threading
import time as t
var = 0

logging.basicConfig(level=logging.DEBUG,
                    format='[%(levelname)s] (%(threadName)-10s) %(message)s',
                    )

def testDef(stop_event):
    global var
    print 'Thread Running', var
    # inThread.stop()
    while not stop_event.isSet():
        var += 1
        logging.debug('Var is %i' % var) 
        t.sleep(1)

# Use an event to track user input
testStopEvent = threading.Event()
testStopEvent.clear()

test = threading.Thread(name = 'test', target=testDef, args=((testStopEvent,)))
test.setDaemon(True)
while True:
    menu = input("On = 1, Off = 2, Show Var = 3")
    if menu == 1:
        test.start()
    elif menu == 2:
        testStopEvent.set()
        test.join() # Wait for the thread to finish
        test = threading.Thread(target=testDef, args=((testStopEvent,))) # "re-start" thread
        testStopEvent.clear() # Reset the stop event

    elif menu == 3:
        print(var)

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

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