Python Forum
How to Host a REST API that executes a PYTHON script on the WEB SERVER - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: How to Host a REST API that executes a PYTHON script on the WEB SERVER (/thread-15563.html)



How to Host a REST API that executes a PYTHON script on the WEB SERVER - rahul4data - Jan-22-2019

Hi Python Gurus,
I have Python installed on my machine.
I have created a python script my_python_script.py that executes locally.
Now, I am trying to expose this as a REST API, that can be executed from the Browser URL remotely.
Can you tell me which Web Server I need to install e.g. APACHE / TOMCAT.
How can I build a REST API that executes my_PYTHON_script on the WEB SERVER and responds with an output as the Response.
Can you also include solutions that do not include Flask


RE: How to Host a REST API that executes a PYTHON script on the WEB SERVER - micseydel - Jan-24-2019

Why not Flask? It seems like the ideal solution, and not telling us why might mean our alternative suggestions have the problem(s).


RE: How to Host a REST API that executes a PYTHON script on the WEB SERVER - rahul4data - Jan-25-2019

Hi @micseydel
Trying to look at the alternatives to Flask.
I read Flask has its limitations e.g. it is not ideal for heavy load scenarios e.g. cannot process multiple requests in parallel.


RE: How to Host a REST API that executes a PYTHON script on the WEB SERVER - buran - Jan-25-2019

(Jan-25-2019, 07:47 AM)rahul4data Wrote: cannot process multiple requests in parallel.

Nope, that's not correct
multiple concurrent requests in Flask


RE: How to Host a REST API that executes a PYTHON script on the WEB SERVER - DeaD_EyE - Jan-25-2019

Due the limitation of the GIL, threading is not good for CPU intensive tasks.
They should run in a different process (Multiprocessing).

There are some Modules, which are similar to Flask, but async.
Usually one Thread which handles the requests, is enough.
Most of the time, the WebServer is waiting for IO (sockets, Databases, etc.).

The question is, do you need to handle so many requests per second?
If you have blocking CPU intensive tasks, you should avoid executing them in your WebServer code.
In this case this tasks should be done by other Processes. There are different ways and libraries to handle it.
Multiprocessing together with message queues can handle your CPU intensive tasks.

The benefit is, that async code looks like synchronous code. But you have always to remind, never call blocking functions.
Calling blocking functions in async code, blocks your whole WebServer. Even time.sleep(x) have to be replaced by await asyncio.sleep(x)

I could not remind the name of the other framework. It is also similar, but they are going a different way.

Then you can use also other Frameworks:

Django has also support for asyncio. They made a new standard called ASGI.

There are still mature frameworks like Tornado and Twisted which also supports async programming.