K
K
kkolorid2019-10-12 18:18:49
Python
kkolorid, 2019-10-12 18:18:49

Why can't Docker server handle aiohttp requests?

Good day. I'm trying to use this Python application - https://github.com/rahiel/open_nsfw-- It is written in Python, uses the aiohttp library and is built/run via Docker according to the manual. If I raise this whole thing on a virtual machine and make one-time requests from a computer, everything works. Even if you run the script in a VM in a cycle from several consoles, it works. But when I try to send requests to the server from my Python application via aiohttp - the server often gives a 504 error and generally does not work adequately. Docker answers:

Error handling request
Error handling request
Error handling request
^C======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7face6e61d68>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7facde557388>, 5138.691)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde68eb40>, 4843.989)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde557c10>, 5124.785)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde68ea08>, 5135.462)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facf7ed0800>, 5124.289)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde52ece0>, 5130.191)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde557590>, 5137.217)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde5cf6c8>, 5128.721)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde59ca08>, 5119.731)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde52e0b0>, 5112.963), (<aiohttp.client_proto.ResponseHandler object at 0x7facde512c10>, 5113.93)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde52ef50>, 5133.297)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde7999a0>, 5136.193)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7face40f4ad8>, 5137.71)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde5574c0>, 5129.21)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde878250>, 4843.473)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde52e800>, 5107.494), (<aiohttp.client_proto.ResponseHandler object at 0x7facde52ea70>, 5107.516), (<aiohttp.client_proto.ResponseHandler object at 0x7facde52e590>, 5111.026), (<aiohttp.client_proto.ResponseHandler object at 0x7facde52eba8>, 5136.718)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde5cf5f8>, 5119.217), (<aiohttp.client_proto.ResponseHandler object at 0x7facde6f5048>, 5126.76)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde52ea08>, 5121.286)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7face409d6c8>, 5130.681)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde5570b0>, 5126.269)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde512d48>, 5122.311)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde52cb40>, 5120.225)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde557528>, 5134.891)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7face48152b8>, 5108.08)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde512730>, 5107.541), (<aiohttp.client_proto.ResponseHandler object at 0x7facde52c798>, 5133.84)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde504320>, 5138.2)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde52e528>, 5120.744)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7face4815d48>, 5134.35)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde52cdb0>, 5107.515), (<aiohttp.client_proto.ResponseHandler object at 0x7facde52c660>, 5108.589), (<aiohttp.client_proto.ResponseHandler object at 0x7facde7d52b8>, 5123.794)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde52e6c8>, 5125.281)]', '[(<aiohttp.client_proto.ResponseHandler object at 0x7facde59c798>, 5118.149)]']
connector: <aiohttp.connector.TCPConnector object at 0x7face6e61dd8>

Either Docker can't cope with such loads, or the script itself is really "Unclosed client session
client_session: " and you just need to fix something in the repository. Although it is not clear why it works in other cases? ..
Google really says that in such cases it is necessary not to forget to close the aiohttp session somewhere. Here aiohttp in the sources - https://github.com/rahiel/open_nsfw--/blob/master/... - tried to insert session closing in it and rebuilt the project locally - to no avail.
Tried casting with Flask. I made a server on it, through which I simply natively, supposedly through the console, run a script that already addresses the docker on another port - it works, albeit slowly + vm is shocked by the load, sometimes answers fly by. Actually, it doesn't fit either.
I created a question in the repository, but the owner has no time to support the project - https://github.com/rahiel/open_nsfw--/issues/3
The general task is to get the declared functionality in the form of a server with api, to which I can send many simultaneous requests using aiohttp .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2019-10-13
@zenwalker

1. If docker on the server is behind nginx or another proxy, there may be a limitation on the backend response (nginx has 30 seconds by default), after which the connection is broken, and the client receives a 504.
2. aiohttp is applied in the wrong place, called here https://github.com/rahiel/open_nsfw--/blob/master/... the classify() function is not asynchronous and will block the process until it completes. In a word, you have a normal single-threaded application; out of the box, several simultaneous requests will not be able to process such a solution. You need to either run several containers with a balancer in front of them, or somehow change the approach, for example, run the classifier in a separate process pool via loop.run_in_executor()or separate the API and the classifier into different services by putting a message queue between them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question