X
X
Xcopy2015-04-19 22:55:03
Django
Xcopy, 2015-04-19 22:55:03

What is the best way to send HTTP requests to a third-party service in Django?

Hello!
I need to send a GET request to a third-party service through dzhang's views.
At first glance, you can simply do:

r = requests.get("http://example.ru")
print r.status

But as far as I know, Django works in one thread and, for example, while one user sends a GET request and waits for a response, the entire server will "hang" and another user will not even be able to send a request.
Is it so?
And if so, what is the best way to send requests to external services? Through celery?
Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Abramov, 2015-04-20
@perminovma

Unlike Alexei Bukin, you understand almost correctly. Python is single-threaded by default. And if you perform a blocking operation, and all socket operations are blocking, then the entire thread will be blocked. As a rule, the server (uwsgi, gunicorn) launches several threads of execution ( workers ) between which user requests are shared, but nevertheless, in your case, each of them may hang for the duration of the operation on the socket.
There are two ways to deal with this, the first is to execute them in other processes using the same cellery. True, then you have to tinker in order to give the result to the client.
The second is to try to use gevent which will provide the ability to work with sockets asynchronously. You need to dig towards the gevent mode of the uwsgi daemon.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question