Python Event.is_set()方法 (Python Event.is_set() Method)
is_set() is an inbuilt method of the Event class of the threading module in Python.
is_set()是Python中线程模块的Event类的内置方法。
An event class object manages an internal flag whose value can be changed by other methods of this class. is_set() function returns true if the internal flag of an event object is true else returns false.
事件类对象管理一个内部标志,该标志的值可以由此类的其他方法更改。 如果事件对象的内部标志为true,则is_set()函数返回true,否则返回false。
Module:
模块:
from threading import Event
Syntax:
句法:
is_set()
Parameter(s):
参数:
None
没有
Return value:
返回值:
The return type of this method is <class 'bool'>. It returns true if the internal flag of the current event class object is true else returns false.
此方法的返回类型为<class'bool'> 。 如果当前事件类对象的内部标志为true,则返回true,否则返回false。
Example:
例:
# Python program to explain the
# use of is_set() method in Event() class
import threading
import time
def helper_function(event_obj, timeout, i):
# Thread has started, but it will wait 4 seconds
# for the event
print("Thread started, for the event to set")
print("Is the event set to true now?", event_obj.is_set())
flag = event_obj.wait(timeout)
if flag:
print("Event has set to true(), moving ahead with the thread")
else:
print("Time out occured, event internal flag still false. Executing thread without waiting for event")
print("Value to be printed=", i)
if __name__ == '__main__':
# Initialising an event object
event_obj = threading.Event()
# starting the thread who will wait for the event
thread1 = threading.Thread(target=helper_function, args=(event_obj, 4, 30))
thread1.start()
# sleeping the current thread for 5 seconds
time.sleep(5)
# generating the event
event_obj.set()
print("Is the event set to true now?", event_obj.is_set())
print("Event is set to true. Now threads can be released.")
Output:
输出:
Thread started, for the event to set
Is the event set to true now? False
Time out occured, event internal flag still false. Executing thread without waiting for event
Value to be printed= 30
Is the event set to true now? True
Event is set to true. Now threads can be released.
翻译自: https://www.includehelp.com/python/event-is_set-method-with-example.aspx