How to shut down a TCPServer serve_forever() loop?

Marko Rauhamaa marko at pacujo.net
Tue Nov 28 01:04:10 EST 2017


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.


Marko



More information about the Python-list mailing list