Python Event.set() 方法
set() 是 Python 中线程模块的 Event 类的内置方法。
当。。。的时候set()
方法被调用,该事件类对象的内部标志被设置为真。作为set()
方法被一个对象调用,所有等待该事件对象的线程都被唤醒。
模块:
from threading import Event
用法:
set()
参数:
- None
返回值:
这个方法的返回类型是<class 'NoneType'>
.该方法不返回任何内容。它将事件对象的内部标志设置为真。
范例1:
# Python program to explain the
# use of set() method in Event() class
import threading
import time
def helper_function(event_obj, timeout,i):
# Thread has started, but it will wait 10 seconds
# for the event
print("Thread started, for the event to set")
flag = event_obj.wait(timeout)
if flag:
print("Event was set to true() earlier, 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,10,27))
thread1.start()
# sleeping the current thread for 5 seconds
time.sleep(5)
# generating the event
event_obj.set()
print("Event is set to true. Now threads can be released.")
print()
输出:
Thread started, for the event to set Event is set to true. Now threads can be released. Event was set to true() earlier, moving ahead with the thread
范例2:
# Python program to explain the
# use of set() method in Event() class
import threading
import time
def helper_function(event_obj, timeout,i):
# Thread has started, but it will wait 3 seconds
# for the event
print("Thread started, for the event to set")
flag = event_obj.wait(timeout)
if flag:
print("Event was set to true() earlier, 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,3,27))
thread1.start()
# sleeping the current thread for 5 seconds
time.sleep(5)
# generating the event
event_obj.set()
print("Event is set to true. Now threads can be released.")
print()
输出:
Thread started, for the event to set Time out occured, event internal flag still false. Executing thread without waiting for event Value to be printed= 27 Event is set to true. Now threads can be released.
相关用法
- Python Event clear()用法及代码示例
- Python Event is_set()用法及代码示例
- Python Event wait()用法及代码示例
- Python Enumerate()用法及代码示例
- Python numpy.less()用法及代码示例
- Python Sympy Permutation.list()用法及代码示例
- Python Matplotlib.figure.Figure.subplots_adjust()用法及代码示例
- Python numpy.tril()用法及代码示例
- Python Matplotlib.pyplot.matshow()用法及代码示例
- Python __file__用法及代码示例
- Python Pandas Panel.add()用法及代码示例
- Python Matplotlib.axis.Tick.get_window_extent()用法及代码示例
- Python numpy.fromstring()用法及代码示例
- Python random.getstate()用法及代码示例
- Python Scipy integrate.quadrature()用法及代码示例
- Python numpy.random.standard_normal()用法及代码示例
- Python Pandas tseries.offsets.CustomBusinessHour.onOffset用法及代码示例
- Python Matplotlib.pyplot.thetagrids()用法及代码示例
- Python Pandas TimedeltaIndex.memory_usage用法及代码示例
- Python os.path.normcase()用法及代码示例
注:本文由纯净天空筛选整理自 Python Event Class | set() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。