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.mzph.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;乘积最大子数组…