classification
Title: Multiprocessing: multiprocessing.connection.Listener.accept() should accept a timeout
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Tom Cook
Priority: normal Keywords:

Created on 2017-12-07 15:09 by Tom Cook, last changed 2017-12-07 15:12 by Tom Cook.

Messages (2)
msg307809 - (view) Author: Tom Cook (Tom Cook) Date: 2017-12-07 15:09
If nothing connects to it, `multiprocessing.connection.Listener.accept()` will block forever with no good way to interrupt it.

Supposing that a thread implements a loop like this:

    def run(self):
        l = Listener(socket_path, 'AF_UNIX')
        while self.running:
            c = l.accept()
            while self.running:
                data = c.recv()
                self.process(data)

There is no obvious way to implement a `stop` method on this thread.  Setting `self.running = False` may never result in the thread terminating, as it may be that no client connects to it.  The following is a possible way of implementing it:

    def stop(self):
        self.running = False
        try:
            c = Client(socket_path, 'AF_UNIX')
        except:
            pass

however it seems fraught with race conditions.  Letting `accept()` accept a timeout would be a much cleaner solution to this and many similar problems.
msg307810 - (view) Author: Tom Cook (Tom Cook) Date: 2017-12-07 15:12
The same goes for `Connection.recv()`, as in the sample code another case where the thread will never terminate is when a `Client` is connected to the socket but never sends any messages; in this case, the call to `recv()` will block forever.  There is no way at all to interrupt this.
History
Date User Action Args
2017-12-07 15:12:45Tom Cooksetmessages: + msg307810
2017-12-07 15:09:59Tom Cookcreate