Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As Dhaivat said, you can just use your Flask app directly with Gunicorn.</p> <p>If you still want to use Flask-Script, you will need to create a custom <code>Command</code>. I don't have any experience with Gunicorn, but I found a similar <a href="https://bitbucket.org/youngking/flask-actions/src/381d6eea3e78/flaskext/actions/server_actions.py#cl-81" rel="nofollow noreferrer">solution</a> for Flask-Actions and ported it to Flask-Script, although be warned, it's untested.</p> <pre><code>from flask_script import Command, Option class GunicornServer(Command): description = 'Run the app within Gunicorn' def __init__(self, host='127.0.0.1', port=8000, workers=4): self.port = port self.host = host self.workers = workers def get_options(self): return ( Option('-H', '--host', dest='host', default=self.host), Option('-p', '--port', dest='port', type=int, default=self.port), Option('-w', '--workers', dest='workers', type=int, default=self.workers), ) def handle(self, app, host, port, workers): from gunicorn import version_info if version_info &lt; (0, 9, 0): from gunicorn.arbiter import Arbiter from gunicorn.config import Config arbiter = Arbiter(Config({'bind': "%s:%d" % (host, int(port)),'workers': workers}), app) arbiter.run() else: from gunicorn.app.base import Application class FlaskApplication(Application): def init(self, parser, opts, args): return { 'bind': '{0}:{1}'.format(host, port), 'workers': workers } def load(self): return app FlaskApplication().run() </code></pre> <p>You can then either register it to replace Flask's <a href="http://flask.pocoo.org/docs/api/#flask.Flask.run" rel="nofollow noreferrer">local development server</a> at <code>python manage.py runserver</code> </p> <pre><code>manager.add_command("runserver", GunicornServer()) </code></pre> <p>or register as a new command such as <code>python manage.py gunicorn</code></p> <pre><code>manager.add_command("gunicorn", GunicornServer()) </code></pre> <p><strong>Edit June 2016</strong>: With the latest version of Flask-Script, change the method <code>handle</code> with <code>__call__</code>. <a href="https://github.com/smurfix/flask-script/blob/cb36eb94d6ae992458b6a66c6c2a715cddfdc4bd/flask_script/commands.py#L138" rel="nofollow noreferrer">old flask-script</a> vs <a href="https://github.com/smurfix/flask-script/blob/master/flask_script/commands.py#L210" rel="nofollow noreferrer">new flask-script</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload