Created
March 19, 2017 06:40
-
-
Save tonywangcn/6d65b886cf3a2c1a5413290fa54e1746 to your computer and use it in GitHub Desktop.
tasks.py
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 characters
from __future__ import absolute_import | |
from test_celery.celeryapp import app | |
import time | |
import requests | |
from pymongo import MongoClient | |
client = MongoClient('10.211.55.12', 27018) # change the ip and port to your mongo database's | |
db = client.mongodb_test | |
collection = db.celery_test | |
post = db.test | |
@app.task(bind=True, default_retry_delay=10) # set a retry delay, 10 equal to 10s | |
def longtime_add(self, i): | |
print 'long time task begins' | |
try: | |
r = requests.get(i) | |
post.insert({'status': r.status_code,"creat_time": time.time()}) # store status code and current time to mongodb | |
print 'long time task finished' | |
except Exception as exc: | |
raise self.retry(exc=exc) | |
return r.status_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment