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()
  • 相关阅读:
    Uva
    Uva
    Uva
    Uva
    Uva
    Uva
    Uva
    Uva
    第二次结队编程作业
    第三次软件工程作业的总结
  • 原文地址:https://www.cnblogs.com/stin/p/8535142.html
Copyright © 2011-2022 走看看