Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial import

  • Loading branch information...
commit 6e98ad19e7894fc2a403d7cd431d7ad32d8cf339 0 parents
Ask Solem authored
17 .gitignore
@@ -0,0 +1,17 @@
+.DS_Store
+*.pyc
+*~
+.*.sw[po]
+dist/
+*.egg-info
+*.egg
+*.egg/
+doc/__build/*
+build/
+.build/
+pip-log.txt
+.directory
+erl_crash.dump
+*.db
+Documentation/
+.tox/
36 example.py
@@ -0,0 +1,36 @@
+import sys
+
+from flask import Flask, request
+from flaskext.celery import Celery
+
+app = Flask(__name__)
+celery = Celery(app)
+
+
+@celery.task(name="example.add")
+def add(x, y):
+ return x + y
+
+
+@app.route("/")
+def hello_world(x=16, y=16):
+ x = int(request.args.get("x", x))
+ y = int(request.args.get("y", y))
+ res = add.apply_async((x, y))
+ context = {"id": res.task_id, "x": x, "y": y}
+ return """Hello world: \
+ add(%(x)s, %(y)s) = \
+ <a href="/result/%(id)s">%(id)s</a>""" % context
+
+
+@app.route("/result/<task_id>")
+def show_result(task_id):
+ retval = add.AsyncResult(task_id).get(timeout=1.0)
+ return repr(retval)
+
+
+if __name__ == "__main__":
+ if "-w" in sys.argv:
+ celery.Worker().run()
+ else:
+ app.run(debug=True)
0  flaskext/__init__.py
No changes.
70 flaskext/celery.py
@@ -0,0 +1,70 @@
+from __future__ import absolute_import
+
+import os
+
+from flask import g
+
+from celery.datastructures import AttributeDict
+from celery.loaders import default as _default
+from celery.utils import get_full_cls_name
+
+
+
+
+class FlaskLoader(_default.Loader):
+
+ def read_configuration(self):
+ self.configured = True
+ return self.setup_settings(_default.DEFAULT_UNCONFIGURED_SETTINGS)
+os.environ.setdefault("CELERY_LOADER", get_full_cls_name(FlaskLoader))
+
+
+class Celery(object):
+
+ def __init__(self, app):
+ self.app = app
+ self.conf = AttributeDict()
+ self.app.config.setdefault("CELERY_RESULT_BACKEND", "amqp")
+
+ from celery.conf import prepare
+ prepare(self.conf, AttributeDict(self.app.config))
+
+
+ def create_task_cls(self):
+ from celery.backends import default_backend, get_backend_cls
+ from celery.task.base import Task
+ conf = self.conf
+
+ class BaseFlaskTask(Task):
+ app = self.app
+ ignore_result = conf.IGNORE_RESULT
+ serializer = conf.TASK_SERIALIZER
+ rate_limit = conf.DEFAULT_RATE_LIMIT
+ track_started = conf.TRACK_STARTED
+ acks_late = conf.ACKS_LATE
+ backend = get_backend_cls(conf.RESULT_BACKEND)()
+
+ @classmethod
+ def apply_async(self, *args, **kwargs):
+ if not kwargs.get("connection") or kwargs.get("publisher"):
+ kwargs["connection"] = self.establish_connection(
+ connect_timeout=kwargs.get("connect_timeout"))
+ return super(BaseFlaskTask, self).apply_async(*args, **kwargs)
+
+ @classmethod
+ def establish_connection(self, *args, **kwargs):
+ from celery.messaging import establish_connection
+ kwargs["defaults"] = conf
+ return establish_connection(*args, **kwargs)
+
+ return BaseFlaskTask
+
+ def task(self, *args, **kwargs):
+ from celery.decorators import task
+ kwargs.setdefault("base", self.create_task_cls())
+ return task(*args, **kwargs)
+
+ def Worker(self, **kwargs):
+ from celery.bin.celeryd import Worker
+ kwargs["defaults"] = self.conf
+ return Worker(loglevel="INFO", **kwargs)
35 setup.py
@@ -0,0 +1,35 @@
+"""
+Flask-Celery
+------------
+
+Celery integration for Flask
+
+"""
+from setuptools import setup
+
+setup(
+ name='Flask-Celery',
+ version='2.1',
+ url='http://github.com/ask/flask-celery/',
+ license='BSD',
+ author='Ask Solem',
+ author_email='ask@celeryproject.org',
+ description='Celery integration for Flask',
+ long_description=__doc__,
+ packages=['flaskext'],
+ namespace_packages=['flaskext'],
+ zip_safe=False,
+ platforms='any',
+ install_requires=[
+ 'Flask'
+ ],
+ classifiers=[
+ 'Environment :: Web Environment',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: BSD License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
+ 'Topic :: Software Development :: Libraries :: Python Modules'
+ ]
+)
Please sign in to comment.
Something went wrong with that request. Please try again.