Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python have a similar control mechanism to Java's CountDownLatch?

So, I will start by saying that this is for a homework problem. My professor gave us an assignment which must be written once in Java and once in another language; I chose the second language to be Python since I'm at least a little familiar with it. The program must work in the following way:

start the main method/thread, which we will call parent
start thread child 1 from the parent
start thread grandchild from thread child 1
start thread child 2 from the parent
print grandchild from the grandchild thread
print child 2 from the child 2 thread
print child 1 from the child 1 thread
print parent from the main method/parent thread

These things must be done in this order. I have written code that does this in Java using CountDownLatch in order to organize the way these things occur. However, I didn't see a similar mechanism in Python (although I'm less familiar with Python than Java). Is there a similar mechanism that maybe I just can't find because I don't know what it's called?

like image 346
Gossamer Shadow Avatar asked Apr 19 '12 21:04

Gossamer Shadow


People also ask

What are main methods CountDownLatch class has?

Class declaration CountDownLatch. await() method block the main thread execution until the current count reaches to zero. the count is decremented using countDown() method by executing threads when their task is completed. Any call to await returns immediately once the count is 0.

Why CountDownLatch is used in Java?

CountDownLatch is used to make sure that a task waits for other threads before it starts. To understand its application, let us consider a server where the main task can only start when all the required services have started.

What is the threading in Python?

Threading in python is used to run multiple threads (tasks, function calls) at the same time. Note that this does not mean that they are executed on different CPUs. Python threads will NOT make your program faster if it already uses 100 % CPU time. In that case, you probably want to look into parallel programming.

What is the use of countDown latch?

Class CountDownLatch. A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. A CountDownLatch is initialized with a given count.


2 Answers

you can implement CountDownLatch using threading.Condition like this:

import threading

class CountDownLatch(object):
    def __init__(self, count=1):
        self.count = count
        self.lock = threading.Condition()

    def count_down(self):
        self.lock.acquire()
        self.count -= 1
        if self.count <= 0:
            self.lock.notifyAll()
        self.lock.release()

    def await(self):
        self.lock.acquire()
        while self.count > 0:
            self.lock.wait()
        self.lock.release()
like image 160
kk17 Avatar answered Oct 07 '22 15:10

kk17


Look at the Semaphore or Condition classes from the threading module.

like image 25
Roland Smith Avatar answered Oct 07 '22 14:10

Roland Smith