ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python中是否有替代threading.Condition变量的更好支持超时而不轮询的变量?

2019-11-06 22:56:40  阅读:196  来源: 互联网

标签:multithreading condition-variable python


我在需要超时的线程中使用条件变量.我直到运行大量线程时才看到CPU使用率才注意到,线程模块中提供的条件变量实际上并未休眠,但会在提供超时作为参数时进行轮询.

有没有其他替代方法可以像pthreads一样实际休眠?

似乎让很多线程每隔几秒钟就休眠一次,却让它仍然消耗CPU时间,这似乎很痛苦.

谢谢!

解决方法:

Python中这样做似乎很棘手,但这是一个解决方案.它依赖于产生其他线程,但不使用轮询,并确保在超时到期或原始的wait()返回时立即唤醒原始线程.

注意:以下代码包括一个测试用例,它测试由于超时以及由于通知而导致的条件等待结束.

from thread import start_new_thread
from threading import Condition, Timer

class ConditionWithoutPolling():
    """Implements wait() with a timeout without polling.  Wraps the Condition
    class."""
    def __init__(self, condition):
        self.condition = condition
        self.wait_timeout_condition = Condition()

    def wait(self, timeout=None):
        """Same as Condition.wait() but it does not use a poll-and-sleep method
        to implement timeouts.  Instead, if a timeout is requested two new
        threads are spawned to implement a non-pol-and-wait method."""
        if timeout is None:
            # just use the original implementation if no waiting is involved
            self.condition.wait()
            return
        else:
            # this new boolean will tell us whether we are done waiting or not
            done = [False]

            # wait on the original condition in a new thread
            start_new_thread(self.wait_on_original, (done,))

            # wait for a timeout (without polling) in a new thread
            Timer(timeout, lambda : self.wait_timed_out(done)).start()

            # wait for EITHER of the previous threads to stop waiting
            with self.wait_timeout_condition:
                while not done[0]:
                    self.wait_timeout_condition.wait()

    def wait_on_original(self, done):
        """Waits on the original Condition and signals wait_is_over when done."""
        self.condition.wait()
        self.wait_is_over(done)

    def wait_timed_out(self, done):
        """Called when the timeout time is reached."""
        # we must re-acquire the lock we were waiting on before we can return
        self.condition.acquire()
        self.wait_is_over(done)

    def wait_is_over(self, done):
        """Modifies done to indicate that the wait is over."""
        done[0] = True
        with self.wait_timeout_condition:
            self.wait_timeout_condition.notify()

    # wrap Condition methods since it wouldn't let us subclass it ...
    def acquire(self, *args):
        self.condition.acquire(*args)
    def release(self):
        self.condition.release()
    def notify(self):
        self.condition.notify()
    def notify_all(self):
        self.condition.notify_all()
    def notifyAll(self):
        self.condition.notifyAll()

def test(wait_timeout, wait_sec_before_notification):
    import time
    from threading import Lock
    lock = Lock()
    cwp = ConditionWithoutPolling(Condition(lock))
    start = time.time()

    def t1():
        with lock:
            print 't1 has the lock, will wait up to %f sec' % (wait_timeout,)
            cwp.wait(wait_timeout)
        time_elapsed = time.time() - start
        print 't1: alive after %f sec' % (time_elapsed,)        

    # this thread will acquire the lock and then conditionally wait for up to 
    # timeout seconds and then print a message 
    start_new_thread(t1, ())

    # wait until it is time to send the notification and then send it
    print 'main thread sleeping (will notify in %f sec)' % (wait_sec_before_notification,)
    time.sleep(wait_sec_before_notification)
    with lock:
        cwp.notifyAll()
        print 'notification sent, will continue in 2sec'
    time.sleep(2.0) # give the other time thread to finish before exiting

if __name__ == "__main__":
    print 'test wait() ending before the timeout ...'
    test(2.0, 1.0)

    print '\ntest wait() ending due to the timeout ...'
    test(2.0, 4.0)

标签:multithreading,condition-variable,python
来源: https://codeday.me/bug/20191106/2001388.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有