multi request handling parallelly by using flask - python

#app.route("/")
def start():
#will do some task
return 'completed'
In the above program, after execution the 1st request 2nd request will execute. But I want to make such a server that will accept, execute and response multiple requests at a certain time parallelly by using flask or anything else.
How will I make this?

For multi-request handling/production deployment, gunicorn or apache or gevent has to be used.
http://flask.pocoo.org/docs/0.11/deploying/
Similar approach follows for other python web frameworks too like Django.

You can use klein module, which handle multiple requests in a time.
Please refer the following lin which will give clear explanation
about limiting in FLASK.
Comparison between Flask and Klein
After refering this link I switched from Flask to Klein. Hope it helps you too.

Related

Run a perl script from within Flask Web Application

I have a Flask Web Application that is periodically receiving JSON information from another application via HTTP POST.
My Flask Web Application is running on a CentOS 7 Server with Python 2.7.X.
I am able to parse the fields from this received JSON in the Flask Web Application and get some of the information that interests me. For example: I get some JSON input and extract an "ID":"7" field from it.
What I want to do now is run a perl script from within this Flask Web Application by using this "ID":"7".
Running 'perl my_perl_script.pl 7' manually on the command line works fine. What I want is for the Flask Web Application to perform this automatically whenever it receives an HTTP POST, by using the specific ID number found in this POST.
How can I do that in Flask?
Is it a good idea to do it with a subprocess call or should I consider implementing queues with Celery/rq? Or maybe some other solution?
I think the perl script should be invoked as a separate Linux process, independent of the Flask Web Application.
Thank you in advance :)
Sub,
I vote yes on subprocess, here's a post on SO about it. Control remains with Flask that way. An alternative might be to code a perl script that watchdogs for a trigger event depending on your needs, but that would put more of the process control on the perl side of things and less efficient use of resources.

How can I monitoring a flask app without modifying his code?

I've been making a little system to monitoring a flask app and others (postgres database, linux server, etc) with prometheus. Everything is going well, but I would like monitoring my flask app without modifying the code.
For example to monitoring methods of my app I did:
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
#app.route('/')
#REQUEST_TIME.time()
def index():
myUser = User.query.all()
return render_template('add_user.html', myUser= myUser)
I used this python library.
Also, I used other library to monitoring a flask app:
monitor(app, port=9999)
unfortunately both are modifying my code. I want to monitoring my flask app without modifying his code. It is possible?
It really isn't clear what you're asking. However, if you just need info about which requests are called and how long they take, you could run your flask app with newrelic: they offer a free tier (yay!) and using it you will gain lots of insight about your app. however to use it you'll need to run your app with their client (but no code changes are required).
more info on what you'll get can be found here:
https://newrelic.com/python/flask

How to unittest aiohttp.web applications

Given an aiohttp.web application with views like this one:
async def hello(request):
return web.Response(body=b"Hello, world")
I'm trying to understand how to properly unit-test them.
I normally use Django's own test client when writing Django apps, and was looking for something similar for aiohttp.web. I'm not sure this is the right approach or not.
TL;DR: How do I simulate a request to an aiohttp.web app in a unittest?
You may create web server and perform real HTTP requests without simulation.
See https://github.com/KeepSafe/aiohttp/blob/master/tests/test_client_functional.py for example.

Web application with python

I have a Python program that takes one input (string) and prints out several lines as an output. I was hoping to get it online, so that the web page only has a text-area and a button. Inserting a string and pressing the button should send the string to python and make the original page print out the output from Python.
Assuming I have only dealt with Python and a little bit of HTML and have very limited knowledge about the web, how might I approach this problem?
For some reason, my first instinct was PHP, but Googling "PHP Python application" didn't really help.
That is, assuming what I want IS a 'web application', right? Not a web server, web page or something else?
How could I get a really simple version (like in the first paragraph) up and running, and where to learn more?
Yes, the term you're looking for is "web application". Specifically, you want to host a Python web application.
Exactly what this means, can vary. There are two main types of application - cgi and wsgi (or stand-alone, which is what most people talk about when they say "web app").
Almost nobody uses cgi nowadays, you're much more likely to come across a site using a web application framework, like Django or Flask.
If you're worried about easily making your page accessible to other people, a good option would be to use something like the free version of Heroku do to something simple like this. They have a guide available here:
https://devcenter.heroku.com/articles/getting-started-with-python#introduction
If you're more technically inclined and you're aware of the dangers of opening up your own machine to the world, you could make a simple Flask application that does this and just run it locally:
from flask import Flask, request
app = Flask(__name__)
#app.route('/')
def main():
return 'You could import render_template at the top, and use that here instead'
#app.route('/do_stuff', methods=['POST'])
def do_stuff():
return 'You posted' + request.form.get('your string')
app.run('127.0.0.1', port=5555, debug=True)
Pick a Python web development framework and follow the getting started tutorial. Main options are Django for a "batteries included" approach or Flask for a minimalistic bottom-up approach.

Creating a python web server to recieve XML HTTP Requests

I am currently working on a project to create simple file uploader site that will update the user of the progress of an upload.
I've been attempting this in pure python (with CGI) on the server side but to get the progress of the file I obviously need send requests to the server continually. I was looking to use AJAX to do this but I was wondering how hard it would be to, instead of changing to some other framerwork (web.py for instance), just write my own web server for receiving the XML HTTP Requests?
My main problem is that sending the request is done from HTML and Javascript so it all seems like magic trickery at the moment.
Can anyone advise me as to the best way to go about receiving these requests on the server?
EDIT: It seems that a framework would be the way to go. Would web.py be a good route to take?
I would recommend to use a microframework like Sinatra for Ruby. There seem to be some equivalents for Python. What python equivalent of Sinatra would you recommend?
Such a framework allows you to simply map a single method to a route.
Writing a very basic HTTP server won't be very hard (see http://docs.python.org/library/simplehttpserver.html for an example), but you will be missing many features that are provided by real servers and web frameworks.
For your project, I suggest you pick one of the many Python web frameworks and run your application behind Apache/mod_wsgi.
There's absolutely no need to write your own web server. Plenty of options exist, including lightweight ones like nginx.
You should use one of those, and either your own custom WSGI code to receive the request, or (better) one of the microframeworks like Flask or Bottle.

Categories

Resources