Can I serve multiple clients using just Flask app.run() as standalone?
flask.Flask.run
accepts additional keyword arguments (**options
) that it forwards to werkzeug.serving.run_simple
- two of those arguments are threaded
(a boolean) and processes
(which you can set to a number greater than one to have werkzeug spawn more than one process to handle requests).
threaded
defaults to True
as of Flask 1.0, so for the latest versions of Flask, the default development server will be able to serve multiple clients simultaneously by default. For older versions of Flask, you can explicitly pass threaded=True
to enable this behaviour.
For example, you can do
if __name__ == '__main__':
app.run(threaded=True)
to handle multiple clients using threads in a way compatible with old Flask versions, or
if __name__ == '__main__':
app.run(threaded=False, processes=3)
to tell Werkzeug to spawn three processes to handle incoming requests, or just
if __name__ == '__main__':
app.run()
to handle multiple clients using threads if you know that you will be using Flask 1.0 or later.
That being said, Werkzeug's serving.run_simple
wraps the standard library's wsgiref
package - and that package contains a reference implementation of WSGI, not a production-ready web server. If you are going to use Flask in production (assuming that "production" is not a low-traffic internal application with no more than 10 concurrent users) make sure to stand it up behind a real web server (see the section of Flask's docs entitled Deployment Options for some suggested methods).
How to create standalone using Flask 0.10 micro framework to be run at a particular time
If you have a standalone cron job the better practice is to avoid using any sort of logic in the job that involves the Flask request context/object. The test_request_context
context is just for testing and shouldn't be used in task setup.
I don't know what your view function looks like, but you should abstract away the "task creation" part of the function into its own method that lives independently. That the cron job and view function can both share it.
@app.route('/task_list')
def get_task_list():
data = request.args.get('tasks')
# some sort of logic happening here
ret = []
for word in data:
ret.append('some task')
return jsonify(data=ret)
After
def convert_data_to_tasks(data):
ret = []
for word in tasks:
ret.append('some task')
return ret
@app.route('/task_list')
def get_task_list():
tasks = request.args.get('tasks')
ret = convert_data_to_tasks(tasks)
return jsonify(ret)
Related Topics
Is This Bad Programming Practice in Tkinter
Python Script for Minifying CSS
Typeerror: Use() Got an Unexpected Keyword Argument 'Warn' When Importing Matplotlib
Bloomberg Server API and Ruby/Python
What Is the "Sys.Stdout.Write()" Equivalent in Ruby
How to Validate a Date String Format in Python
How to Use Cookies in Python Requests
How to Put Parameterized SQL Query into Variable and Then Execute in Python
Common Xlabel/Ylabel for Matplotlib Subplots
What's the Difference Between "Pip Install" and "Python -M Pip Install"
Error Message: 'Chromedriver' Executable Needs to Be Path
How to Change the Styles of Pandas Dataframe Headers
Plotting of 2D Data:Heatmap with Different Colormaps
What's the Ruby Equivalent of Python's Os.Walk
What Programming Language Features Are Well Suited for Developing a Live Coding Framework
How to Exchange Keys with Values in a Dictionary