python线程的暂停与继续_Python-是否有可能“停止”或“暂停”一个线程

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. I have indented correctly, however, SO seems to have messed it up a bit)

Basically, what I want is:

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.

Does anyone have any ideas?

解决方案

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

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.

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.

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值