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 |

python set |_Python事件类| set()方法与示例相关推荐

  1. python中如何调用类的方法

    python中如何调用类的方法 类的方法的调用: 与普通的函数调用类似 1.类的内部调用:self.<方法名>(参数列表). 2.在类的外部调用:<实例名>.<方法名&g ...

  2. python新式类c3算法_Python新式类的方法解析顺序MRO与Super

    新式类与经典类的方法解析顺序 MOR(方法解析顺序) 经典类:深度优先 DFS python3以前 新式类:广度优先 python2.2 新式类:广度优先的C3算法实现(拓扑排序) BFS pytho ...

  3. python编程-迭代器(类,方法,继承),函数,数据结构,

    a, b = 0, 1 while b < 10:print(b) #print(b,end=',')a, b = b, a+b相当于 n=b m=a+b a=n b=m输出: 1 1 2 3 ...

  4. python代码大全和用法用量_Python生成器的使用方法和示例代码

    本文是<Effect Python 编写高质量Python代码的59个有效方法>的学习笔记.主要记录生成器的使用方法和示例代码. 返回队列的函数 如果函数要产生一系列结果,那么最简单的做法 ...

  5. python学习笔记:类的方法总结

    python中类的方法总结 在python中,类的方法有如下三种: (1)实例方法(即:对象方法) (2)类方法 (3)静态方法 下面,将对这三种方法进行总结. 1.实例方法(对象方法) 通常情况下, ...

  6. python中append的用法_Python 列表 append() 使用方法及示例

    Python 列表 append() 使用方法及示例 append()方法将一个项目添加到列表的末尾. append()方法将单个项目添加到列表的末尾. append()方法的语法为:list.app ...

  7. python中remove用法_Python 列表 remove() 使用方法及示例

    Python 列表 remove() 使用方法及示例 remove()方法从列表中删除第一个匹配元素(作为参数传递). remove()方法的语法为:list.remove(element) remo ...

  8. python字典更新值_Python 字典 update() 使用方法及示例

    Python 字典 update() 使用方法及示例 update()方法向字典插入指定的项目.这个指定项目可以是字典或可迭代对象. 如果键不在字典中,则update()方法将元素添加到字典中.如果键 ...

  9. python三级_Python三级目录展示方法

    要求: 1.三级菜单 2.可依次选择进入各子菜单,选择序号进入目录 3.输入b返回上级目录,q退出更改目录 代码实现: #!/bin/env python #!--*--coding:utf-8 -- ...

最新文章

  1. 非科班出身获得软开职位
  2. 《Access 2007开发指南(修订版)》一一1.5 什么是数据库对象
  3. 双机之间的串行通信设计 c语言编程,双机串行通信设计精品.doc-资源下载人人文库网...
  4. 计划任务 at,cron
  5. 关于arguments的用法
  6. 里能嵌jquery吗_白芸豆里的淀粉阻断成分,能帮助减肥是真的吗?
  7. 2018-2019-1 20165320 《信息安全系统设计基础》第八周学习总结
  8. 腾讯云的云数据库MYSQL配置
  9. linux修改主机名(不重启)
  10. *args和**kargs
  11. crossentropy java_示例CrossEntropyLoss用于pytorch中的3D语义分段
  12. 图片不存放文件夹,直接存在数据库中,
  13. Facebook广告收费出价方式之cpi
  14. 计算机专业的入门书籍(第一篇博客)
  15. 一文读懂高频交易程序化交易和量化交易区别
  16. 笔记本不小心网络重置后,不能上网,网络适配器存在感叹号
  17. PPT写的好,升职加薪不会少-全套免费教程
  18. -ile “……的“ 形容词后缀
  19. 中序遍历二叉树非递归
  20. 又到年末“团建”!某企业员工吐槽:这真是一场噩梦……

热门文章

  1. HTML5新增的主体结构元素
  2. java调用kettle例子_Kettle API - Java调用示例
  3. jquery 操作css 选择器
  4. yearProgress.vue
  5. CSS 学习路线(二)选择器
  6. DIV固定宽度和动态拉伸混合水平排列
  7. Vue表格中,对数据进行转换、处理
  8. webpack3的CommonsChunkPlugin插件详解
  9. bind搭建(二)反向解析
  10. Python 06-字典