This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2017-12-07 15:09 by Tom Cook, last changed 2022-04-11 14:58 by admin.

Messages (3)
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.
msg378675 - (view) Author: Carlo Dri (carlodri) * Date: 2020-10-15 11:18
In agree with this proposal! I was looking for the exact same possibility of specifying a timeout to the accept() method, both in AF_INET and AF_PIPE sockets. If there is any  better way to implement this I would love to hear it.
History
Date User Action Args
2022-04-11 14:58:55adminsetgithub: 76425
2020-10-15 11:21:51carlodrisetversions: + Python 3.8
2020-10-15 11:18:26carlodrisetnosy: + carlodri
messages: + msg378675
2017-12-07 15:12:45Tom Cooksetmessages: + msg307810
2017-12-07 15:09:59Tom Cookcreate