Created
May 31, 2016 09:56
-
-
Save lukassup/fdf8362985ddff782db4afac8171482f to your computer and use it in GitHub Desktop.
This file contains 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 characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""\ | |
Start RabbitMQ server: | |
$ sudo systemctl start rabbitmq.service | |
Start Celery worker: | |
$ celery worker -A tasks -l debug | |
Launch Python interactively, add some tasks and verify the output: | |
$ python | |
>>> from tasks import get_json | |
>>> tasks = [] | |
>>> tasks.append(get_json.delay("https://pypi.python.org/pypi/celery/json")) | |
>>> tasks[0].get() | |
""" | |
from celery import Celery | |
import urllib.request as request | |
import json | |
import os | |
BASE_DIR = os.path.realpath(__file__) | |
REQUIRE_AUTH = False | |
app = Celery() | |
app.conf.CELERY_RESULT_BACKEND = "db+sqlite:///celery-results.db" | |
app.conf.CELERY_RESULT_PERSISTENT = True | |
app.conf.CELERYBEAT_ENABLE_UTC = True | |
app.conf.CELERYBEAT_LOG_LEVEL = "DEBUG" | |
app.conf.CELERY_ACCEPT_CONTENT = ["json"] | |
app.conf.CELERY_TASK_SERIALIZER = "json" | |
app.conf.CELERYBEAT_TIMEZONE = "UTC" | |
if REQUIRE_AUTH: | |
auth_handler = request.HTTPBasicAuthHandler() | |
auth_handler.add_password(realm=None, | |
uri="https://localhost:8080/", | |
user="USER", | |
passwd="PASS") | |
opener = request.build_opener(auth_handler) | |
request.install_opener(opener) | |
else: | |
request.install_opener(request.build_opener()) | |
@app.task | |
def get_json(url): | |
with request.urlopen(url) as response: | |
return json.loads(response.read().decode("UTF-8")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment