How to shut down a TCPServer serve_forever() loop?

Chris Angelico rosuav at gmail.com
Tue Nov 28 04:21:45 EST 2017


On Tue, Nov 28, 2017 at 5:04 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Ian Kelly <ian.g.kelly at gmail.com>:
>
>> On Sat, Nov 25, 2017 at 7:10 AM, John Pote <johnpote at jptechnical.co.uk> wrote:
>>> The issue is that if I press a key on the keyboard the key is
>>> immediately shown on the screen but then the shutdown() call blocks
>>> until another TCP connection is made, text is echoed back and only
>>> then does serve_forever()return followed by shutdown()returning as
>>> can be seen from the console session,
>> [...]
>> Make a connection to the server after calling shutdown to wake up the
>> server's event loop? I'm guessing it only checks the shutdown flag
>> after responding to an event, so there's probably not much else you
>> could do.
>
> Seems to be one of the fundamental multithreading issues: each thread is
> blocked on precisely one event. Asyncio is more flexible: you can
> multiplex on a number of events.

Not really, no. Unless select() counts as "precisely one event", of course.

http://man7.org/linux/man-pages/man2/select.2.html
https://en.wikipedia.org/wiki/Select_(Unix)

That's the normal way for a thread to block on multiple events on a
Unix system. Windows has its own approximate equivalent.

Surprise, surprise, that's also how event loops often implemented.
Including ones used in packages like asyncio.

ChrisA



More information about the Python-list mailing list