线程同步对象_使用事件对象进行线程同步

线程同步对象

It's time to learn more about threads in python. In this tutorial we will be covering an important classe, the Event class which is used for thread synchronization in python.

现在是时候了解有关python中线程的更多信息了。 在本教程中,我们将介绍一个重要的类,即Event类,该类用于python中的线程同步。

This class is used for inter thread communication by generating events.

此类通过生成事件用于线程间通信。

Python多线程:事件对象 (Python Multithreading: Event Object)

The Event class object provides a simple mechanism which is used for communication between threads where one thread signals an event while the other threads wait for it. So, when one thread which is intended to produce the signal produces it, then the waiting thread gets activated.

Event类对象提供了一种简单的机制,该机制用于线程之间的通信,其中一个线程发出事件信号,而其他线程等待事件。 因此,当一个旨在产生信号的线程产生信号时,等待线程就被激活。

An internal flag is used by the event object known as the event flag which can be set as true using the set() method and it can be reset to false using the clear() method.

内部标志由事件对象使用,称为事件标志 ,可以使用set()方法将其设置为true,并可以使用clear()方法将其重置为false。

The wait() method blocks a thread until the event flag for which it is waiting is set true by any other thread..

wait()方法将阻塞线程,直到其他线程将其等待的事件标志设置为true。

Following are the useful functions used along with an event object:

以下是与事件对象一起使用的有用功能:

初始化事件对象 (Initialize Event object)

We can initialize an Event object as follows:

我们可以如下初始化一个Event对象:

import threading

are_you_coming = threading.Event()

When we initialize an event object like this the internal flag is set to false by default.

当我们像这样初始化一个事件对象时,默认情况下内部标志设置为false

isSet()方法 (isSet() method)

This method returns true if and only if the internal flag is true.

当且仅当内部标志为true时,此方法才返回true。

import threading

are_you_coming = threading.Event()
print(are_you_coming.isSet())

False

set()方法 (set() method)

When this method is called for any event object then the internal flag is set to true. And as soon as set() methos is called for any event all threads waiting for it are awakened.

当对任何事件对象调用此方法时,内部标志将设置为true。 一旦针对任何事件调用set()方法,所有等待它的线程都会被唤醒。

clear()方法 (clear() method)

This method resets the internal flag to false. Subsequently, threads calling wait() on the event for which clear() is called, it will block until the internal flag is not true again.

此方法将内部标志重置为false。 随后,线程在调用了clear()的事件上调用wait() ,它将阻塞直到内部标志再次不为真。

wait([timeout])方法 (wait([timeout]) method)

When we have to make any thread wait for an event, we can do so by calling this method on that event which has the internal flag set to false, doing so blocks the thread until the internal flag is true for the event.

当我们必须让任何线程等待事件时,可以通过在将内部标志设置为false的事件上调用此方法来这样做,从而阻塞线程,直到事件的内部标志为true为止。

If the internal flag is true on entry, then the thread will never get blocked. Otherwise, it is blocked until another thread calls set() to set the flag to true, or until the optional timeout occurs. The timeout argument specifies a timeout for the operation in seconds.

如果内部标志在进入时为true,则该线程将永远不会被阻塞。 否则,它将被阻塞,直到另一个线程调用set()将标志设置为true,或者直到发生可选的超时为止。 timeout参数以秒为单位指定操作的超时。

实例时间 (Time for an Example)

Let's have a simple code example to demonstrate the usage of Event class object.

让我们用一个简单的代码示例来演示Event类对象的用法。

In the code below we will create a thread and make it wait for an event which will be generated by the main thread, releasing the first thread.

在下面的代码,我们将创建一个线程,使其等待将由线程中产生,释放的第一个线程的事件。

演示地址

In the above program we have also used the timeout property of the wait() method.

在上面的程序中,我们还使用了wait()方法的timeout属性。

When a thread calls wait([timeout]) method, the method returns a boolean value true if the wait is released on receiving the event object, else it returns false if the wait is released due to timeout.

当线程调用wait([timeout])方法时,如果在接收事件对象时释放了等待,则该方法返回布尔值true ;否则,如果由于超时而释放了等待,则该方法返回false

To test this, change the value of timeout sent as argument args=(e,4) on line 18 and keep it less than the sleep time, for example set the timeout value to 2 and see the output.

要对此进行测试,请更改第18行上作为参数args=(e,4)发送的timeout的值,并使其小于睡眠时间,例如,将timeout值设置为2并查看输出。

翻译自: https://www.studytonight.com/python/python-threading-event-object

线程同步对象

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值