日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python set |_Python事件类| set()方法与示例

發布時間:2023/12/1 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python set |_Python事件类| set()方法与示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 timedef 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 objectevent_obj = threading.Event()# starting the thread who will wait for the eventthread1 = threading.Thread(target=helper_function, args=(event_obj,10,27))thread1.start()# sleeping the current thread for 5 secondstime.sleep(5)# generating the eventevent_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 timedef 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 objectevent_obj = threading.Event()# starting the thread who will wait for the eventthread1 = threading.Thread(target=helper_function, args=(event_obj,3,27))thread1.start()# sleeping the current thread for 5 secondstime.sleep(5)# generating the eventevent_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 |

總結

以上是生活随笔為你收集整理的python set |_Python事件类| set()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。