Content:
尽管flask的官网给出了celery的相关函数,但那个似乎只对单独实例有效,如果flask的实例是使用工厂函数创建,则celery很容易会遇到循环导入,或者是没有上下文等情况。
celery是需要启动两个实例,一个是使用命令行启动worker,一个则由flask实例在创建时celery实例
Flask官方样例
其他方法
以下方式是我目前使用的,可参考模板。
1. 目录结构
|-main/
|-blueprints/
|- __init__.py
|- views.py
|-static/
|-templates/
|- __init__.py
|- extensions.py
|-config.py
|-venv/
|-celery_worker.py
|-app.py
2. 各文件代码
app.py
from main import create_app
app = create_app()
celery = app.celery
if __name__ == "__main__":
app.run()
main.__init__.py
import os
from main.configs import config
from main.blueprints.view import f_bp
def make_celery(app):
from main.extensions import celery
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery
def create_app(config_name=None):
if config_name is None:
config_name = os.getenv('FLASK_CONFIG', 'development')
app =Flask(__name__)
app.config.from_object(config[config_name])
app.celery = make_celery(app)
register_blueprints(app)
return app
def register_blueprints(app):
app.register_blueprint(f_bp)
extensions.py
from main.configs import config
from celery import Celery
celery = Celery(
broker=config[config_name].CELERY_BROKER_URL,
backend=config[config_name].result_backend)
CELERY_RESULT_BACKEND
参数即将被弃用,改为result_backend
views.py
from flask.blueprints import Blueprint
from tasks import add_together
f_bp = Blueprint("dashboard", __name__)
@f_bp.route('/test_celery')
def test_celery():
task = add_together.apply_async(args=[23, 42], retry=False)
return task.get()
celery_worker.py
from main.extensions import celery
@celery.task(name='sum-of-two-numbers')
def add_together(a, b):
return a + b
3. 启动worker
celery -A app.celery worker --loglevel=info --pool=eventlet
参考文档
https://segmentfault.com/q/1010000006266773
https://github.com/mrchi/flask-with-celery-example
https://github.com/stefanondisponibile/flask_celery
There are 3 comments
Mitzi
2021-06-30T13:15:15ZHi , I am following up on my message below. Who would I speak with about handling your US order fulfillment and shipping? Regards, Mitzi order-fulfillment.net ------------------------------------------------------------------------ Hi, Who would I speak with at your company that manages your product shipping and order fulfillment? We are US company, offering warehousing, order fulfillment and drop shipping to our customers since 2005. Here are some of the items we ship for clients: -Books, training manuals, guides -E-com product drop shipping -New member welcomes boxes and gifts -Product samples -Health and Medical supplements -Marketing materials -Medical program test kits -Follow up gifts to clients, leads, and prospects Do you have some time to discuss - phone / email ? Thanks, Fulfillment Specialist www.Order-Fulfillment.net
Jami
2021-05-19T19:31:02ZAre you looking for the best way to market your business? FbCourses.net has the answer.. Not sure where to start, or what to do? Why not learn from the best. All of the best. We are offering at never before seen prices all of the top courses for one insanely low price. If you have been thinking of how to generate more leads, website traffic or sales today is the day. All courses are available in full with immediate download on FbCourses.net Not sure which one you like, for the next 24 hours we are offering the entire suite of courses for the lowest amount you could possible imagine. Check us out : FbCourses.net You could be driving new income or starting the new side hustle today!
Tamera
2021-01-22T23:32:40ZIt is with sad regret to inform you StarDataGroup.com is shutting down. It has been a tough year all round and we decided to go out with a bang! Any group of databases listed below is $49 or $149 for all 16 databases in this one time offer. You can purchase it at www.StarDataGroup.com and view samples. - LinkedIn Database 43,535,433 LinkedIn Records - USA B2B Companies Database 28,147,835 Companies - Forex Forex South Africa 113,550 Forex Traders Forex Australia 135,696 Forex Traders Forex UK 779,674 Forex Traders - UK Companies Database 521,303 Companies - German Databases German Companies Database: 2,209,191 Companies German Executives Database: 985,048 Executives - Australian Companies Database 1,806,596 Companies - UAE Companies Database 950,652 Companies - Affiliate Marketers Database 494,909 records - South African Databases B2B Companies Database: 1,462,227 Companies Directors Database: 758,834 Directors Healthcare Database: 376,599 Medical Professionals Wholesalers Database: 106,932 Wholesalers Real Estate Agent Database: 257,980 Estate Agents Forex South Africa: 113,550 Forex Traders Visit www.stardatagroup.com or contact us with any queries. Kind Regards, StarDataGroup.com