Skip to content

Instantly share code, notes, and snippets.

@dongweiming
Last active August 29, 2015 14:09
Show Gist options
  • Save dongweiming/97b92e5f69c1e8915491 to your computer and use it in GitHub Desktop.
Save dongweiming/97b92e5f69c1e8915491 to your computer and use it in GitHub Desktop.
settings

启动celery

celery -A app.celery worker

启动flask

python app.py

来个双层requests.get

python test.py

# coding=utf-8
from __future__ import absolute_import
from os import environ
from flask import Flask
import settings
import requests
from celery import Celery
app = Flask(__name__)
app.config.from_object(settings)
def make_celery(app):
celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'],
backend=app.config['CELERY_RESULT_BACKEND'])
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
celery = make_celery(app)
@celery.task(name="tasks.fetch_dongxi")
def fetch_dongxi(id):
url = 'http://dongxi.douban.com/show/{}'.format(id)
print 'Fetch: {}'.format(url)
return requests.get(url).text[1100:1210]
@app.route("/result/<id>")
def show_result(id):
retval = fetch_dongxi.apply_async((id,)).get(timeout=5.0)
return repr(retval)
if __name__ == "__main__":
port = int(environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port, debug=True)
# coding=utf-8
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/1'
# coding=utf-8
import requests
for i in range(2008385, 2008485):
print requests.get("http://localhost:5000/result/{}".format(i)).text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment