zoukankan      html  css  js  c++  java
  • Python 之Event

    线程间互相等状态。

    import threading
    import time
    import logging
    
    logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-10s) %(message)s',)
    
    def worker(event):
        logging.debug('Waiting for redis ready...')
        event.wait()
        logging.debug('redis ready, and connect to redis server and do some work [%s]', time.ctime())
        time.sleep(1)
    
    def main():
        readis_ready = threading.Event()
        t1 = threading.Thread(target=worker, args=(readis_ready,), name='t1')
        t1.start()
    
        t2 = threading.Thread(target=worker, args=(readis_ready,), name='t2')
        t2.start()
    
        logging.debug('first of all, check redis server, make sure it is OK, and then trigger the redis ready event')
        time.sleep(3) # simulate the check progress
        readis_ready.set()  # 该event置为set状态,等待该event的线程可以运行
    
        # readis_ready.isSet()
        # readis_ready.clear()
    
    if __name__=="__main__":
        main()
  • 相关阅读:
    leetcode 39 Combination Sum
    C/C++ 单元测试 catch
    二叉树
    线性表
    POJ1002
    HDU4329
    hdu 4329
    java代码优化总结1
    Linux操作系统常用命令总结1
    java开发基础知识总结1
  • 原文地址:https://www.cnblogs.com/stin/p/8535142.html
Copyright © 2011-2022 走看看