Created
August 21, 2014 19:04
-
-
Save anonymous/fa47834db2f4f3b8b257 to your computer and use it in GitHub Desktop.
Revisions
-
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ # Empty This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ import os basedir = os.path.abspath(os.path.dirname(__file__)) class Config: @staticmethod def init_app(app): pass class LocalConfig(Config): DEBUG = True SQLALCHEMY_DATABASE_URI = r"sqlite:///" + os.path.join(basedir, "data-dev.sqlite") CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//' config = { "local": LocalConfig} SELECTED_CONFIG = "local" This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy() This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ from extensions import db from celery import Celery from flask import Flask from flask import g from config import config, SELECTED_CONFIG 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 celery.app = app return celery def create_before_request(app): def before_request(): g.db = db return before_request def create_app(): app = Flask(__name__) app.config.from_object(config[SELECTED_CONFIG]) db.init_app(app) #celery = create_celery_app(app) # Register the blueprints here # Add the before request handler app.before_request(create_before_request(app)) return app This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ from factory import create_app import tasks app = create_app() @app.route('/test', methods=['POST']) def task_simple(): tasks.do_some_stuff.delay() return "" if __name__ == "__main__": app.run() This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ from extensions import db class User(db.Model): __tablename__ = "user" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(128), unique=True, nullable=False) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ from factory import create_celery_app from celery.signals import task_prerun from flask import g from extensions import db celery = create_celery_app() @task_prerun.connect def celery_prerun(*args, **kwargs): #print g with celery.app.app_context(): # # use g.db print db print g #@celery.task(base=celery.Task) @celery.task() def do_some_stuff(): print db print g # run with: celery.exe worker -A tasks