python中线程同步_条件对象-Python中的线程同步

python中线程同步

In order to synchronize the access to any resources more efficiently, we can associate a condition with tasks, for any thread to wait until a certain condition is met or notify other threads about the condition being fulfilled so that they may unblock themselves.

为了更有效地同步对任何资源的访问,我们可以将条件与任务关联,让任何线程等待直到满足某个条件,或者将满足条件的情况通知其他线程,以便它们可以解除阻塞。

Let's take a simple example to understand this. In the Producer Consumer problem, if there is one Produces producing some item and one Consumer consuming it, then until the Producer has produced the item the Consumer cannot consume it. Hence the Consumer waits until the Produced produces an item. And it's the duty of the Producer to inform the Consumer that an item is available for consumption once it is successfully produced.

让我们举一个简单的例子来理解这一点。 在生产者消费者问题中 ,如果有一个生产者生产某种物品,而有一个消费者消费该物品,那么在生产者生产了该物品之前,消费者不能消费它。 因此,消费者要等到生产者生产商品为止。 生产者有责任告知消费者,一旦产品成功生产,便可以消费。

And if there are multiple Consumers consuming the item produced by the Producer then the Producer must inform all the Consumers about the new item produced.

并且,如果有多个消费者消费生产者生产的物品,那么生产者必须将生产的新物品告知所有消费者。

This is a perfect usecase for the condition object in multithreading in python.

这是python多线程中条件对象的理想用例。

条件对象: wait()notify()notifyAll() (Condition object: wait(), notify() and notifyAll())

Now that we know what the condition object is used for in python multithreading, let's see the syntax for it:

现在我们知道了条件对象在python多线程中的用途,让我们看一下它的语法:

condition = threading.Condition([lock])

The condition object takes in an optional lock object as argument. If we do not provide anything then it creates a default lock.

条件对象接受一个可选的锁对象作为参数。 如果我们不提供任何内容,那么它将创建一个默认锁。

A condition object has acquire() and release() methods that call the corresponding methods of the associated lock. It also has a wait() method, and notify() and notifyAll() methods. These three must only be called after the calling thread has acquired the lock.

条件对象具有acquire()release()方法,它们调用关联锁的相应方法。 它还具有一个wait()方法以及notify()notifyAll()方法。 仅在调用线程获取了锁之后才调用这三个对象。

条件类方法 (Condition class methods)

Following are condition class methods:

以下是条件类方法:

acquire(*args)方法 (acquire(*args) method )

This method is used to acquire the lock. This method calls the corresponding acquire() method on the underlying lock present in the condition object; The return value is whatever that method returns.

此方法用于获取锁。 该方法在条件对象中存在的基础锁上调用相应的acquire()方法。 返回值是该方法返回的值。

release()方法 (release() method)

this method is used to release the lock. This method calls the corresponding release() method on the underlying lock present in the condition object.

此方法用于释放锁定。 此方法在条件对象中存在的基础锁上调用相应的release()方法。

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

This method is used to block the thread and make it wait until some other thread notifies it by calling the notify() or notifyAll() method on the same condition object or until the timeout occurs.

此方法用于阻塞线程,并使其等待,直到其他线程通过在同一条件对象上调用notify()notifyAll()方法来通知该线程,或者直到发生超时。

This must only be called when the calling thread has acquired the lock.

仅当调用线程获得了锁时才可以调用此方法。

When called, this method releases the lock and then blocks the thread until it is awakened by a notify() or notifyAll() call for the same condition variable from some other thread, or until the timeout occurs.

调用此方法时,该方法将释放锁定,然后阻塞线程,直到被来自其他线程的相同条件变量的notify()notifyAll()调用唤醒该线程为止,或者直到发生超时为止。

This method returns True if it is released because of notify() or notifyAll() method else if timeout occurs this method will return False boolean value.

如果由于notify()notifyAll()方法而被释放,则此方法返回True ,否则,如果发生超时,则此方法将返回False布尔值。

notify()方法 (notify() method)

It wakes up any thread waiting on the corresponding condition. This must only be called when the calling thread has acquired the lock. Also, calling this method will wake only one waiting thread.

它唤醒等待相应条件的所有线程。 仅当调用线程获得了锁时才可以调用此方法。 同样,调用此方法将仅唤醒一个等待线程。

notifyAll()方法 (notifyAll() method)

It wakes up all the threads waiting on this condition. This method acts like notify() method, but wakes up all the waiting threads instead of one.

它唤醒所有在这种情况下等待的线程。 此方法的行为类似于notify()方法,但是唤醒所有等待线程而不是一个线程。

时间为例! (Time for an Example!)

In the code example below we have implemented a simple producer-consumer solution, where the producer produces an item and adds it to a list from which the consumer is consuming the items.

在下面的代码示例中,我们实现了一个简单的生产者-消费者解决方案,其中生产者生产一个商品并将其添加到消费者正在消费该商品的列表中。

演示地址

Few important takeaways from the code example above:

上面的代码示例中没有几个重要的要点:

  1. We have created a class SomeItem which has a list which acts as the shared resource between the producer and consumer thread.

    我们创建了一个SomeItem类, SomeItem具有一个list ,该list充当生产者线程和使用者线程之间的共享资源。

  2. The producer thread is randomly generating some list items and adding it to the list.

    生产者线程随机生成一些列表项并将其添加到列表中。

  3. The consumer thread tries to consume the item, if item is not found, it starts to wait. If the producer sends a notification to the consumer about the item creation before its timeout, then the consumer consumes the item else it exits due to timeout.

    使用者线程尝试消耗该物品,如果找不到该物品,它将开始等待。 如果生产者在其超时之前向消费者发送有关商品创建的通知,则消费者将消费该商品,否则由于超时而退出。

This is a very simple example covering all the usecases of condition object. Try running the above program with 2 consumer threads and single producer thread.

这是一个非常简单的示例,涵盖了条件对象的所有用例。 尝试使用2个使用者线程和1个生产者线程运行以上程序。

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

python中线程同步

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值