title: How to use Flask-Script and Gunicorn
body: <p>I'm working on on a Flask app using Flask's built in dev server.  I start it using Flask-Script.  I want to switch to using Gunicorn as the web server.  To do so, do I need to write some sort of integration code between Flask-Script and Gunicorn?  Or is Flask-Script irrelevant to running the app using Gunicorn?</p>
  
  <p>Thanks in advance!</p>
  
  <p>Props to @sean-lynch. The following is working, tested code based on his answer.
  The changes I made were:</p>
  
  <ul>
  <li><p>Options that aren't recognized by Gunicorn are removed from <code>sys.argv</code> in <code>remove_non_gunicorn_command_line_args()</code> before trying to start the server.  Otherwise Gunicorn throws an error with a message like this: <code>error: unrecognized arguments: --port 5010</code>.  I remove <code>-p</code> because, even though it doesn't cause the error, that's only because Gunicorn thinks its the short form of its <code>pidfile</code> option, which is obviously not what's intended.</p></li>
  <li><p>GunicornServer.handle() signature modified to match the method it overrides i.e. Command.handle()</p></li>
  </ul>
  
  <p>-</p>
  
  <pre><code>from flask_script import Command
  from gunicorn.app.base import Application
  
  class GunicornServer(Command):
  
      description = 'Run the app within Gunicorn'
  
      def __init__(self, host='127.0.0.1', port=8000, workers=6):
  
          self.port = port
          self.host = host
          self.workers = workers
  
      def get_options(self):
          return (
              Option('-t', '--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, *args, **kwargs):
  
          host = kwargs['host']
          port = kwargs['port']
          workers = kwargs['workers']
  
          def remove_non_gunicorn_command_line_args():
              import sys
              args_to_remove = ['--port','-p']
              def args_filter(name_or_value):
                  keep = not args_to_remove.count(name_or_value)
                  if keep:
                      previous = sys.argv[sys.argv.index(name_or_value) - 1]
                      keep = not args_to_remove.count(previous)
                  return keep
              sys.argv = filter(args_filter, sys.argv)
  
          remove_non_gunicorn_command_line_args()
  
          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:
              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()
  
  manager.add_command('gunicorn', GunicornServer())
  </code></pre>