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

python 示例

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

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

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

When the clear() method is called, the internal flag of that event class object is set to false. As the clear() method gets called for an object, all the threads calling wait() will block until set() is called to set the internal flag true again.

调用clear()方法时,该事件类对象的内部标志设置为false。 当为对象调用clear()方法时,所有调用wait()的线程都将阻塞,直到调用set()再次将内部标志设置为true为止。

Module:

模块:

    from threading import Event

Syntax:

句法:

    clear()

Parameter(s):

参数:

  • None

    没有

Return value:

返回值:

The return type of this method is <class 'NoneType'>. The method does not return anything. It only sets the internal flag of the current event object to false.

此方法的返回类型为<class'NoneType'> 。 该方法不返回任何内容。 它仅将当前事件对象的内部标志设置为false。

Example:

例:

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

def helper_function(event_obj, timeout, i):
  print("Thread started, and event is also set to true")
  # Sleeping for 8 second()
  time.sleep(8)
    
  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, 7, 30))
  # generating the event and setting to true
  event_obj.set()
  thread1.start()
  time.sleep(2)

  # Setting the event internal flag to false
  event_obj.clear()
  print("Event is set to false by clear() method")

Output:

输出:

Thread started, and event is also set to true
Event is set to false by clear() method
Time out occured, event internal flag still false. Executing thread without waiting for event
Value to be printed= 30


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

python 示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值