Putting the stream to sleep until event X occurs

I write for many files in a threaded application, and I create one handler for each file. I have a HandlerFactory class that controls the distribution of these handlers. What I would like to do is that

thread A requests and receives the file file foo.txt from the HandlerFactory class

thread B requests the file handler foo.txt

the handler class recognizes that this file descriptor has been extracted

handler class puts thread A to sleep

thread B closes the file descriptor using the wrapper method from HandlerFactory

HandlerFactory notifies sleeping threads

thread B wakes up and successfully receives the file with the file foo.txt

This is what I still have

def get_handler(self, file_path, type):
    self.lock.acquire()
    if file_path not in self.handlers:
        self.handlers[file_path] = open(file_path, type)
    elif not self.handlers[file_path].closed:
        time.sleep(1)
    self.lock.release()
    return self.handlers[file_path][type]

, , , .

+5
3

, , .

Python 2.

Python 3

+5

, threading.Semaphore, ( , , ). ( , BoundedSemaphore: , , , ; -).

1 ( , ). acquire , ( ), release , ( ). , /// , , , :

, . , . , .

(, : N, 0 N-1 [[]] , ).

+2

, Python , , ?

, - , . . , , . , , XMLRPC sqlite ( ).

-1

All Articles