线程事件用于线程控制线程,线程事件的初始值为False
set:将线程事件的值设为True
clear:将线程事件的值设为False
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import threading import time event = threading.Event() def MyEvent(): count = 1 event.set() while True: if count >5 and count <10: event.clear() #清空标志位 print("Event Have Been Clean!") elif count >10: event.set() #设置标志位 count = 0 print("Event Have Been Set!") else: print("This is a event test!") time.sleep(1) count +=1 def MyEvent_Test(): while True: if event.is_set(): #如果标志位被设置 print("[The Event Is Set]") time.sleep(1) else: print("[The Event Isn‘t Set]") event.wait() #等待标志位被设置 print("The Event Been Set Again") Event_1 = threading.Thread(target=MyEvent) Event_1.start() Event_2 = threading.Thread(target=MyEvent_Test) Event_2.start()
时间: 2024-11-05 21:49:34