Gunicorn TypeError __call__() takes from 1 to 2 positional arguments but 3 were given with Flask application Factory

Viewed 304

I have been searching through for days now trying to find an answer to why this is happing. I have a Flask application using the application factory method. Everything runs fine via the Flask development server, so now I'm trying to deploy with Gunicorn as recommended. I have done this before and it has seemed to work, but that wasn't using the application factory method of instantiating my app. So I'm wondering what I'm doing wrong here.

I have tried several of the suggestions I have found of different ways to run the gunicorn command but nothing seems to work. The workers start up just fine, but as soon as I perform a request via google to any one of my endpoints (mostly the simple GET request to render the initial page) I get the following traceback that occurs.

[2021-04-26 14:37:34 -0500] [25609] [INFO] Worker exiting (pid: 25609)
[2021-04-26 14:37:34 -0500] [25608] [INFO] Shutting down: Master
[2021-04-26 14:37:34 -0500] [25608] [INFO] Reason: Worker failed to boot.
(learn_flask_sqlalchemy) [aspea002@ilscha03-ioad-01 learn_flask_sqlalchemy]$ ls
app.py  config.py  migrations  __pycache__  README.md  requirements.txt  student_directory  wsgi.py
(learn_flask_sqlalchemy) [aspea002@ilscha03-ioad-01 learn_flask_sqlalchemy]$ gunicorn "student_directory:create_app()"
[2021-04-26 14:38:00 -0500] [25796] [INFO] Starting gunicorn 20.1.0
[2021-04-26 14:38:00 -0500] [25796] [INFO] Listening at: http://127.0.0.1:8000 (25796)
[2021-04-26 14:38:00 -0500] [25796] [INFO] Using worker: sync
[2021-04-26 14:38:00 -0500] [25797] [INFO] Booting worker with pid: 25797
/home/aspea002/miniconda3/envs/learn_flask_sqlalchemy/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py:851: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
  warnings.warn(
/home/aspea002/miniconda3/envs/learn_flask_sqlalchemy/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py:872: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  warnings.warn(FSADeprecationWarning(
[2021-04-26 14:38:06 -0500] [25797] [ERROR] Error handling request /
Traceback (most recent call last):
  File "/home/aspea002/miniconda3/envs/learn_flask_sqlalchemy/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 136, in handle
    self.handle_request(listener, req, client, addr)
  File "/home/aspea002/miniconda3/envs/learn_flask_sqlalchemy/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 179, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: __call__() takes from 1 to 2 positional arguments but 3 were given
[2021-04-26 14:38:07 -0500] [25797] [ERROR] Error handling request /favicon.ico
Traceback (most recent call last):
  File "/home/aspea002/miniconda3/envs/learn_flask_sqlalchemy/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 136, in handle
    self.handle_request(listener, req, client, addr)
  File "/home/aspea002/miniconda3/envs/learn_flask_sqlalchemy/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 179, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: __call__() takes from 1 to 2 positional arguments but 3 were given

Here is my directory structure:

enter image description here

__init__.py file that you see has the following contents:

import os
import logging
import sys

from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_restful import Api
from flask_script import Manager, Server
from flask_sqlalchemy import SQLAlchemy
# from flask_apscheduler import APScheduler
# from student_directory import jobs
from student_directory.students.models import db


def create_app():
    student_app = Flask(__name__)
    student_app.config.from_object(os.environ.get('app_env'))
    db.init_app(student_app)
    manager = Manager(student_app)
    migrate = Migrate(student_app, db)
    manager.add_command('db', MigrateCommand)
    # scheduler = APScheduler()
    # scheduler.init_app(student_app)
    # scheduler.start()
    student_api = Api(student_app)
    if 'runserver' in sys.argv:
        manager.add_command('runserver', Server(use_debugger=student_app.config.get('DEBUG'),
                                                threaded=student_app.config.get('THREADED'), port=student_app.config.get('PORT'),
                                                host=student_app.config.get('HOST'),
                                                use_reloader=student_app.config.get('USE_RELOADER')))
    with student_app.app_context():
        from . import routes

    return manager

I have tried to do some of the suggestion that you can call the create_app() method directly:

gunicorn -w 2 "student_directory:create_app()"

I have also tried the wsgi.py file approach as well with the contents of that file being:

from student_directory import create_app

student_app = create_app()

With gunicorn command gunicorn -w 2 wsgi:student_app.

But no matter what I do every request keeps hitting this exception in the sync.py file of gunicorn and I'm not sure why.

Any advice would be great cause at this point I'm assuming it has something with what I am doing or I need to be doing differently with the latest version of gunicorn. I have used it previously like a said about a year or so ago and it worked fine so I'm not sure what has changed so drastically according to the Gunicorn release notes update.

0 Answers
Related