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 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 |

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378187.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378187.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378187.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

js 命名規范

轉載于:https://www.cnblogs.com/zjx2011/p/3165043.html

爬蟲項目(四)---采集從01月22日以來全國各省疫情數據

采集從03月02日以來全國各省疫情數據 當然&#xff0c;數據來源仍然是丁香園新型冠狀病毒肺炎疫情實時動態首頁 url&#xff1a;https://ncov.dxy.cn/ncovh5/view/pneumonia 分析 確定01月22日以來全國各省疫情數據的URL 由項目(三)可以獲取全國各省疫情數據點擊可下載&…

Install PHP and Apache

http://cn.php.net/manual/en/install.unix.apache2.php Install Apache first, then sudo apt-get install libxml2-dev sudo apt-get install libmysqlclient16-dev Then, configure PHP轉載于:https://www.cnblogs.com/songsiyao/archive/2011/09/15/2178087.html

糾錯碼trick和數據壓縮trick

糾錯碼和壓縮算法是同一枚硬幣的兩面。 兩者都來自于對冗余的想法。 糾錯碼被視為向消息或文件中添加冗余的原則性方法。而壓縮算法正好相反&#xff0c;他們會從消息或文件中移除冗余。 壓縮和糾錯并不是彼此抵消的&#xff0c;相反&#xff0c;好的壓縮算法會移除抵消冗余&am…

計算機專業理論,計算機專業綜合理論.doc

計算機專業綜合理論2010年南京市單招班教學調研測試卷(二)計算機專業綜合理論命題人&#xff1a;管榮平 陳高峰 戴則萍 吳有俊本試卷分第一卷(單項選擇題、判斷題)和第二卷(填空題、程序閱讀題、編程題和計算作圖題)兩部分。第一卷1至2頁&#xff0c;第二卷3至6頁。兩卷滿分300…

網站上flv,MP4等格式的視頻文件播放不出來的解決辦法

在做一個網站時&#xff0c;發現視頻文件&#xff0c;比如flv&#xff0c;MP4格式在本地可以正常的播放&#xff0c;但是傳到了開發機器上&#xff0c;就不行了。播放器的文件地址是對的&#xff0c;就是一直沒有反應。 經過長時間的實驗&#xff0c;發現問題在與iis的設置問題…

centos6 更新數據源

嘗試了很多,還是163源最舒服. 編輯yum配置文件(163源)&#xff1a; #vi /etc/yum.repos.d/CentOS-Base.repo [base] nameCentOS-$releasever - Base mirrorlisthttp://mirrorlist.centos.org/?release$releasever&arch$basearch&repoos #baseurlhttp://mirror.centos…

常用算法總結(窮舉法、貪心算法、遞歸與分治算法、回溯算法、數值概率算法)

博主聯系方式&#xff1a; QQ:1540984562 QQ交流群&#xff1a;892023501 群里會有往屆的smarters和電賽選手&#xff0c;群里也會不時分享一些有用的資料&#xff0c;有問題可以在群里多問問。 目錄1、窮舉法2、貪心算法3、遞歸與分治算法4、回溯算法5、數值概率算法1、窮舉法…

struct/class的數據對齊---簡單解析

網上教程一大堆&#xff0c;我這邊就不再贅述廢話了 思路方法&#xff1a; 1&#xff0c;以四個為一組&#xff0c;最終的內存所占結果必須是四的倍數 2&#xff0c;優先考慮四的整數倍&#xff0c;之后再考慮內存空間問題 struct Beyond{int a;char b;short c;}; int mai…

工程師英語和計算機證書查詢,點擊進入國家硬件維修工程師證書查詢網站

工程師證書查詢網站人力資源社會保障部指定查詢國家職業資格證書的唯一官方網站。涵蓋全國各省市、各行業、各央企頒發的證書。電腦硬件維修工程師網上能查看國家工信部硬件維修工程師證書查詢網址&#xff1a;http://www.ceiaec.org/index.htm工程師證書編號在網上怎么查詢如果…

stl vector 函數_vector :: at()函數以及C ++ STL中的示例

stl vector 函數C vector :: at()函數 (C vector::at() function) vector::at() is a library function of "vector" header, it is used to access an element from specified position, it accepts a position/index and returns the reference to the element at…

敏捷開發“松結對編程”系列之七:問題集之一

本文是“松結對編程”系列的第七篇。&#xff08;之一&#xff0c;之二&#xff0c;之三&#xff0c;之四&#xff0c;之五&#xff0c;之六&#xff0c;之七&#xff0c;之八&#xff09;剛剛參加完MPD 2011深圳站&#xff0c;在演講中間及后來媒體采訪&#xff0c;被問到了一…

powerdesigner 導出數據庫表結構

http://www.360doc.com/content/12/0817/19/61497_230730771.shtml轉載于:https://www.cnblogs.com/gaohuag/p/3169095.html

C++中的sort函數對二維數組排序是按照什么準則?

遇到的一個疑惑&#xff0c;現記錄如下&#xff1a; int main() {vector<vector<int>> envelopes { {5, 8},{6, 7},{6, 4},{2, 3},{8,9} };sort(envelopes.begin(), envelopes.end());for (int i 0;i < envelopes.size();i)cout << envelopes[i][0]<…

Exercises

I. Faulty sentences 1&#xff0c;Our host entertained us with many interesting stories of adventure, he has been a member of an exploration team working in the Arctic. 翻譯&#xff1a;我們的主持人用許多有趣的冒險故事來娛樂我們&#xff0c;他是北極探險團隊…

數學專業學計算機哪一行,計算數學

計算數學(一個理科專業)語音編輯鎖定討論上傳視頻計算數學是由數學、物理學、計算機科學、運籌學與控制科學等學科交叉滲透而形成的一個理科專業。中文名計算數學外文名Computational Mathematics所 屬數學計算數學專業定義編輯語音計算數學也叫做數值計算方法或數值分析。主…

數論之數字根 杭電1013

做這道題就有一種感覺&#xff0c;&#xff0c;數學真是奇妙&#xff0c;&#xff0c;在網上查了一下&#xff0c;才知道數字根有那么多奇妙的性質。不過&#xff0c;對于這道題我卻是不太理解&#xff0c;&#xff0c;主要是不會證明為什么數字根就是各個位加起來對9取余&…

ubuntu12.10下安裝mysqlworkbench出現“Dependency is not satisfiable: libctemplate0”問題的解決方案...

(原文地址&#xff1a;http://www.cnblogs.com/Deasel-s-magic-box/p/3169790.html) 之前在window下面一直用navicat&#xff0c;轉到ubuntu下之后&#xff0c;雖然也找到一個navicat的linux版&#xff0c;但是經常各種莫名其妙的掛掉&#xff0c;而且界面實在是挫的1B 。 所以…

圖片透視變換操作

由于照相機硬件設備本身的誤差&#xff0c;可能會導致鏡頭畸變&#xff0c;從而導致照相機拍攝到的照片產生失真現象&#xff0c;此時可以通過透視變換去適當的校正。 大概的思路&#xff1a;在原圖像上確定四個點&#xff0c;然后再新圖像上也確定四個點&#xff0c;通過warp…

dp筆記:關于DP算法和滾動數組優化的思考

從網上總結了一些dp的套路以及對滾動數組的一些思考&#xff0c;現記錄如下&#xff0c;希望以后回顧此類算法時會有所幫助。 目錄1、DP算法經驗1、DP算法核心&#xff1a;2、DP算法類別以及例題例1&#xff1a;三步問題例2&#xff1a;最小路徑和例3&#xff1a;乘積最大子數組…