python wait方法_Python事件类| 带有示例的wait()方法

python wait方法

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

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

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

When we want a thread to wait for an event, we can call the wait() method on that event whose internal flag is set to false, thereby blocking the thread until the internal flag of that event object is set to true by the set() method. If the internal flag is true on entry, then the thread is not blocked. The thread remains blocked until the internal flag remains false or the timeout occurs. We can give the optional timeout to the function with the help of the timeout argument.

当我们想要一个线程等待一个事件,我们可以调用wait()的该事件,其内部标志设置为false,直到事件对象的内部标志被设置为true,从而阻塞线程的方法设定( )方法。 如果内部标志在进入时为true,则不会阻塞线程。 线程将保持阻塞状态,直到内部标志保持为false或发生超时为止。 我们可以借助timeout参数为函数提供可选的超时时间。

Module:

模块:

    from threading import Event

Syntax:

句法:

    wait(timeout=None)

Parameter(s):

参数:

  • timeout: It is an optional parameter, which specifies the timeout for the wait() method. Once this value exceeds, the threads, waiting on the event, get unblocked. Its default value is None.

    timeout :这是一个可选参数,用于指定wait()方法的超时时间。 一旦超过该值,等待事件的线程将被解除阻塞。 其默认值为无。

Return value:

返回值:

The return type of this method is <class 'Bool'>. It returns if the thread gets released before the timeout else returns false.

此方法的返回类型为<class'Bool'> 。 如果线程在超时之前被释放,则返回它,否则返回false。

Example 1:

范例1:

# Python program to explain the
# use of wait() 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 wait() 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-wait-method-with-example.aspx

python wait方法

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值