Python Forum
How to Host a REST API that executes a PYTHON script on the WEB SERVER
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Host a REST API that executes a PYTHON script on the WEB SERVER
#1
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
Reply
#2
Why not Flask? It seems like the ideal solution, and not telling us why might mean our alternative suggestions have the problem(s).
Reply
#3
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.
Reply
#4
(Jan-25-2019, 07:47 AM)rahul4data Wrote: cannot process multiple requests in parallel.

Nope, that's not correct
multiple concurrent requests in Flask
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Script does not work on Linux server scrapemasta 0 105 Mar-22-2024, 11:07 AM
Last Post: scrapemasta
  Choosing Python web host? Crododo 2 2,267 Jan-19-2023, 02:05 PM
Last Post: prvncpa
  deploying python script on a server rickyrt 0 1,439 Aug-31-2021, 04:03 PM
Last Post: rickyrt
  Python Rest API Public/Secret Keys Authentication Nonce dn237 1 2,869 Oct-31-2019, 02:07 AM
Last Post: dn237
  Python Flask REST API vndywarhol 1 2,955 Sep-28-2018, 12:43 PM
Last Post: thomasp
  Begginer - How to choose a server to run my Python script with MongoDB? Mapl 1 3,710 Oct-24-2017, 01:02 PM
Last Post: DeaD_EyE
  Please give me some advice on how to host python apps on a vps FonCheato 1 3,296 Feb-02-2017, 02:21 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020