I can't really help you because I don't know aiohttp, but I note that you
have way too much code manipulating a main loop. I see three separate
loop.run_*() calls and a loop.stop() call that smells funny (because it's
called before the loop is even started). A better idiom would be to put all
this logic (whatever it is) in a couroutine and just run that single
coroutine, so you'd get something like this:
def http_server_thread():
loop.run_until_complete(my_main_coroutine())
loop.close()
@coroutine
my_main_coroutine():
...all the rest of your logic, using yield from to run coroutines...
On Sun, Apr 6, 2014 at 7:48 PM, Lars Andersson <[email protected]> wrote:
>
> Hi,
>
> I'm having some problems to properly shut down an aiohttp server running
> in a separate thread...
>
> The following code is run as a separate python thread to start and stop an
> aiohttp server:
>
> def http_server_thread()
> f = loop.create_server(aiohttp.server.ServerHttpProtocol, ...)
> srv = loop.run_until_complete(f)
> loop.run_forever()
> srv.close()
>
> log.debug("waiting for server to exit...")
> loop.run_until_complete(srv.wait_closed())
> loop.stop()
> loop.run_forever()
>
> loop.close()
> log.error("server thread EXIT")
>
> The server is configured to serve a predefined number of requests, then
> shut it self down by calling self._loop.stop() from within it's request
> handler coroutine (this is used for testing purposes)
>
> Accessing the http server running 'Thread-2' from MainThread generates the
> following events:
>
> MainThread: send GET request to http server running in Thread-2
> Thread-2: REQ01: method = GET; path = /test; transport=139969646978160
> (sock=14)
> Thread-2: Max number or requests served, stopping (by calling
> self._loop.stop())
> Thread-2: New HttpServer Instance: config = {'maxRequests': 2}
> Thread-2: waiting for server to exit...
> Thread-2: closing loop 139969647425744
> Thread-2: server thread EXIT
> Dummy-9: Uncompleted request.
>
> The "Uncompleted request" message is printed by the aiohttp server code,
> and must have been scheduled to run by the server running in "Thread-2",
> but the final message is being printed from another thread, "Dummy-9".
>
> What is going on here? Is this something I should be worried about?
>
> Is it valid for a server to shut itself down by calling loop.stop() in a
> request handler?
>
> In general, what's the recommended way to make sure everything in an event
> loop has been completed before closing it?
>
> Sorry if my explanation of the problem wasn't very clear. I can try to
> make a simpler reproduction code snippet if needed.
>
>
--
--Guido van Rossum (python.org/~guido)