python set |
Python Event.set()方法 (Python Event.set() Method)
set() is an inbuilt method of the Event class of the threading module in Python.
set()是Python中线程模块的Event类的内置方法。
When the set() method is called, the internal flag of that event class object is set to true. As the set() method gets called for an object, all the threads waiting for that event object get awakened.
调用set()方法时,该事件类对象的内部标志设置为true。 当set()方法被一个对象调用时,所有等待该事件对象的线程都被唤醒。
Module:
模块:
from threading import Event
Syntax:
句法:
set()
Parameter(s):
参数:
None
没有
Return value:
返回值:
The return type of this method is <class 'NoneType'>. The method does not return anything. It sets the internal flag of an event object to true.
此方法的返回类型为<class'NoneType'> 。 该方法不返回任何内容。 它将事件对象的内部标志设置为true。
Example 1:
范例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()
Output:
输出:
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
Example 2:
范例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()
Output:
输出:
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.
翻译自: https://www.includehelp.com/python/event-set-method-with-example.aspx
python set |