Python事件类| is_set()方法与示例

Python Event.is_set()方法 (Python Event.is_set() Method)

is_set() is an inbuilt method of the Event class of the threading module in Python.

is_set()是Python中线程模块的Event类的内置方法。

An event class object manages an internal flag whose value can be changed by other methods of this class. is_set() function returns true if the internal flag of an event object is true else returns false.

事件类对象管理一个内部标志,该标志的值可以由此类的其他方法更改。 如果事件对象的内部标志为true,则is_set()函数返回true,否则返回false。

Module:

模块:

    from threading import Event

Syntax:

句法:

    is_set()

Parameter(s):

参数:

  • None

    没有

Return value:

返回值:

The return type of this method is <class 'bool'>. It returns true if the internal flag of the current event class object is true else returns false.

此方法的返回类型为<class'bool'> 。 如果当前事件类对象的内部标志为true,则返回true,否则返回false。

Example:

例:

# Python program to explain the
# use of is_set() method in Event() class
import threading
import time

def helper_function(event_obj, timeout, i):
  # Thread has started, but it will wait 4 seconds 
  # for the event  
  print("Thread started, for the event to set")
  print("Is the event set to true now?", event_obj.is_set())

  flag = event_obj.wait(timeout)
  if flag:
    print("Event has set to true(), 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, 4, 30))
  thread1.start()
  # sleeping the current thread for 5 seconds
  time.sleep(5)
  
  # generating the event
  event_obj.set()
  print("Is the event set to true now?", event_obj.is_set())
  print("Event is set to true. Now threads can be released.")  

Output:

输出:

Thread started, for the event to set
Is the event set to true now? False
Time out occured, event internal flag still false. Executing thread without waiting for event
Value to be printed= 30
Is the event set to true now? True
Event is set to true. Now threads can be released.


翻译自: https://www.includehelp.com/python/event-is_set-method-with-example.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值