I'm implementing an HTTP server using python BaseHttpServer.
I have a queue that is shared by different threads. And each time my handler receives a request, it puts it into the shared queue. When the number of requests in queue reaches n, I'd like to shutdown the server. Here's what I have right now:
while self.queue.qsize() != n
self.server.handle_request()
My concern about this approach is that, it is possible that when checking self.queue.qsize() it's not yet n, but after that the size becomes n immediately (because the queue is shared by multiple threads) and there's no following requests anymore. Given that server.handle_request() blocks if there is no requests, the server may shutdown anymore. Is there a way to solve this problem?