zoukankan      html  css  js  c++  java
  • Python 线程(五):Event

    Event:  是线程同步的一种方式,类似于一个标志,当该标志为false时,所有等待该标志的线程阻塞,当为true时,所有等待该标志的线程被唤醒

    isSet():       当内置标志为True时返回True。 
    set():         将标志设为True,并通知所有处于等待阻塞状态的线程恢复运行状态。 
    clear():       将标志设为False。 
    wait([timeout]):  如果标志为True将立即返回,否则阻塞线程至等待阻塞状态,等待其他线程调用set()

     1 import threading
     2 import time
     3 
     4 event = threading.Event()
     5 
     6 def func():
     7     print "%s is waiting for event...
    " % threading.currentThread().getName()
     8     event.wait()
     9 
    10     print "%s get the Event..
    " % threading.currentThread().getName()
    11 
    12 
    13 if __name__ == "__main__":
    14     t1 = threading.Thread(target=func)
    15     t2 = threading.Thread(target=func)
    16 
    17     t1.start()
    18     t2.start()
    19 
    20     time.sleep(2)
    21 
    22     print "MainThread set Event
    "
    23 
    24     event.set()
    25 
    26     t1.join()
    27     t2.join()
  • 相关阅读:
    vnc安装
    centos下安装图形界面
    granfana telegraf influx安装与使用
    jenkins安装与使用
    yum使用手册
    Python模块--并发相关threading、multiprocessing、Queue、gevent
    Python模块--logging
    Python模块--psutil
    python模块--Beautifulsoup
    Python模块--Pexpect
  • 原文地址:https://www.cnblogs.com/wang-can/p/3582000.html
Copyright © 2011-2022 走看看