Issue
I have 1 laravel server and 1 python flask server that handles machine learning problems. when I send a request to the python server and then python needs to send a request to the laravel server to retrieve the latest data in the laravel server. How to do that without blocking the connection?
Solution
In a traditional HTTP server, you can't. A server thread is assigned for each connection, and it is handling that connection from the time a request is accepted until such time the response has been sent. Any work you need to do between those two points will have the server thread occupied.
There are two basic ways to resolve this. The traditional way was to have a number of worker threads, or even worker processes. Obviously, this is not quite optimal, to say the least.
The other way is to make the server asynchronous, basing it on asyncio
and aiohttp
instead of the more traditional http
or urllib
libraries. If we're speaking of Flask, it has support for async
; or you could use one of the frameworks that is built from start to use the asynchronous model, like Starlette. async
functions can await
for something to happen (e.g. until they receive a response from another server), which frees the executing thread to do something else in the meantime (e.g. accept other connections).
Note that in this case the task you are executing to prepare the response should also be asynchronous. E.g. if you need to execute a program, use e.g. asyncio.create_subprocess_shell
instead of the subprocess.Popen
and the like; and if you want to make a HTTP request, like in your case, you'd use an aiohttp
client instead of the libraries you were using synchronously.
Answered By - Amadan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.