Python-是否可以“停止"或“暂停"一个线程 [英] Python - Is it possible to "stop" or "pause" a thread

查看:424
本文介绍了Python-是否可以“停止"或“暂停"一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个线程,并且我希望一个线程运行10秒钟,然后使该线程停止,而另一个线程执行,然后第一个线程再次启动;重复此过程.所以

I have two threads, and, I want one thread to run for 10 seconds, and then have this thread stop, whilst another thread executes and then the first thread starts up again; this process is repeated. So e.g.

from threading import Thread 
import sys  
import time

class Worker(Thread):

    Listened = False; 

    def __init__(self):

        while 1:
           if(self.Listened == False):
              time.sleep(0)
           else:
            time.sleep(20)

        for x in range(0, 10):
            print "I'm working"
            self.Listened = True

    class Processor(Thread):
        Listened = False;

        def __init__(self):
            # this is where I'm confused!!

Worker().start()
Processer().start()

(P.S.我正确地缩进了,但是,似乎有点弄乱了它)

(P.S. I have indented correctly, however, SO seems to have messed it up a bit)

基本上,我想要的是:

worker线程工作10秒钟左右,然后停止,处理器"启动,并且一旦处理器处理完"Worker"线程的最后一次运行中的数据,然后重新启动该线程. 工人"的线程.我不必专门从当前位置重新启动工人"线程,它可以从头开始.

The worker thread works for 10 seconds (or so) and then stops, the "processor" starts up and, once the processor has processed the data from the last run of the "Worker" thread, it then re-starts the "worker" thread up. I don't specifically have to re-start the "worker" thread from that current position, it can start from the beginning.

有人有什么想法吗?

推荐答案

您可以使用计数信号量以阻止线程,然后稍后将其唤醒.

You can use a counting semaphore to block a thread, and then wake-it-up later.

计数信号量是具有非负整数计数的对象.如果计数为0时,线程在信号量上调用acquire(),则thead将阻塞,直到信号量的计数大于零为止.要解除阻塞线程,另一个线程必须通过在信号量上调用release()来增加信号量的计数.

A counting semaphore is an object that has a non-negative integer count. If a thread calls acquire() on the semaphore when the count is 0, the thead will block until the semaphore's count becomes greater than zero. To unblock the thread, another thread must increase the count of the semaphore by calling release() on the semaphore.

创建两个信号量,一个用于阻止工作程序,另一个用于阻止处理器.因为我们希望它立即运行,所以开始将工作者信号量的计数设为1.将处理器的信号量的计数开始为0,因为我们希望它一直阻塞直到工作人员完成为止.

Create two semaphores, one to block the worker, and one to block the processor. Start the worker semaphore's count a 1 since we want it to run right away. Start the processor's semaphore's count to 0 since we want it to block until the worker is done.

将信号传递给worker和Processor类.工作程序运行10秒后,应通过调用processorSemaphore.release()唤醒处理器,然后应通过调用workerSemaphore.acquire()使其信号量休眠.处理器执行相同的操作.

Pass the semaphores to the worker and processor classes. After the worker has run for 10 seconds, it should wake-up the processor by calling processorSemaphore.release(), then it should sleep on its semaphore by calling workerSemaphore.acquire(). The processor does the same.

#!/usr/bin/env python
from threading import Thread, Semaphore
import sys  
import time

INTERVAL = 10

class Worker(Thread):

    def __init__(self, workerSemaphore, processorSemaphore):
        super(Worker, self).__init__()
        self.workerSemaphore    = workerSemaphore
        self.processorSemaphore = processorSemaphore

    def run(self):
        while True:
            # wait for the processor to finish
            self.workerSemaphore.acquire()
            start = time.time()
            while True:
                if time.time() - start > INTERVAL:
                    # wake-up the processor
                    self.processorSemaphore.release()
                    break

                # do work here
                print "I'm working"

class Processor(Thread):
    def __init__(self, workerSemaphore, processorSemaphore):
        super(Processor, self).__init__()
        print "init P"
        self.workerSemaphore    = workerSemaphore
        self.processorSemaphore = processorSemaphore

    def run(self):
        print "running P"
        while True:
            # wait for the worker to finish
            self.processorSemaphore.acquire()
            start = time.time()
            while True:
                if time.time() - start > INTERVAL:
                    # wake-up the worker
                    self.workerSemaphore.release()
                    break

                # do processing here
                print "I'm processing"

workerSemaphore    = Semaphore(1)
processorSemaphore = Semaphore(0)

worker    = Worker(workerSemaphore, processorSemaphore)
processor = Processor(workerSemaphore, processorSemaphore)

worker.start()
processor.start()

worker.join()
processor.join()

这篇关于Python-是否可以“停止"或“暂停"一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆