python 示例
Python Event.clear()方法 (Python Event.clear() Method)
clear() is an inbuilt method of the Event class of the threading module in Python.
clear()是Python中线程模块的Event类的内置方法。
When the clear() method is called, the internal flag of that event class object is set to false. As the clear() method gets called for an object, all the threads calling wait() will block until set() is called to set the internal flag true again.
调用clear()方法时,该事件类对象的内部标志设置为false。 当为对象调用clear()方法时,所有调用wait()的线程都将阻塞,直到调用set()再次将内部标志设置为true为止。
Module:
模块:
from threading import Event
Syntax:
句法:
clear()
Parameter(s):
参数:
None
没有
Return value:
返回值:
The return type of this method is <class 'NoneType'>. The method does not return anything. It only sets the internal flag of the current event object to false.
此方法的返回类型为<class'NoneType'> 。 该方法不返回任何内容。 它仅将当前事件对象的内部标志设置为false。
Example:
例:
# Python program to explain the
# use of clear() method in Event() class
import threading
import time
def helper_function(event_obj, timeout, i):
print("Thread started, and event is also set to true")
# Sleeping for 8 second()
time.sleep(8)
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, 7, 30))
# generating the event and setting to true
event_obj.set()
thread1.start()
time.sleep(2)
# Setting the event internal flag to false
event_obj.clear()
print("Event is set to false by clear() method")
Output:
输出:
Thread started, and event is also set to true
Event is set to false by clear() method
Time out occured, event internal flag still false. Executing thread without waiting for event
Value to be printed= 30
翻译自: https://www.includehelp.com/python/event-clear-method-with-example.aspx
python 示例