我想从我的芹菜工人那里访问我的postgresql数据库。
由于我正在使用工厂模式,我使用https://github.com/mattupstate/overholt/blob/master/overholt/factory.py这个链接来创建我的芹菜工人。
我正在使用celery -A xyz.jobs worker
命令启动我的芹菜工作者。
jobs.py
档案:
from xyz.models.user import User
from xyz.app import create_celery_app
celery = create_celery_app()
@celery.task()
def do_some_stuff():
print(User.get_all())
当我从其中一个端点调用此任务时,出现以下错误:
[2018-04-09 10:21:39,530: ERROR/ForkPoolWorker-8] Task xyz.jobs.do_some_stuff[b3a71e41-2335-470f-89b8-f4844c6eba1c] raised unexpected: AttributeError("'NoneType' object has no attribute 'drivername'",)
Traceback (most recent call last):
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 999, in __call__
return self.registry[key]
KeyError: 4320768832
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/celery/app/trace.py", line 374, in trace_task
R = retval = fun(*args, **kwargs)
File "/Users/user/Documents/celeryTest/xyz_backend/xyz/app.py", line 30, in __call__
return TaskBase.__call__(self, *args, **kwargs)
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/celery/app/trace.py", line 629, in __protected_call__
return self.run(*args, **kwargs)
File "/Users/user/Documents/celeryTest/xyz_backend/xyz/jobs.py", line 10, in do_some_stuff
print(User.get_all())
File "/Users/user/Documents/celeryTest/xyz_backend/xyz/models/__init__.py", line 47, in get_all
return db.session.query(cls).filter_by(**kwargs).all()
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 153, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 1001, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2950, in __call__
return self.class_(**local_kw)
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 143, in __init__
bind = options.pop('bind', None) or db.engine
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 877, in engine
return self.get_engine()
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 896, in get_engine
return connector.get_engine()
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 556, in get_engine
self._sa.apply_driver_hacks(self._app, info, options)
File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 830, in apply_driver_hacks
if info.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'
我假设,问题出在数据库连接上,但无法解决。有什么我想念的吗?
app.py
档案:
def create_celery_app(app=None):
app = app or create_app()
celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
def create_app():
app = Flask(__name__)
app.config.from_object(Config)
register_extensions(app)
register_api(app)
return app
def register_extensions(app):
CORS(app)
db.init_app(app)
migrate.init_app(app, db)
def register_api(app):
from xyz.api.user import auth_api_blueprint
app.register_blueprint(auth_api_blueprint, url_prefix='/auth')
extensions.py
档案:
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
migrate = Migrate()