Skip to content

Instantly share code, notes, and snippets.

@brizzbane
Last active August 29, 2015 14:20
Show Gist options
  • Save brizzbane/cce5bbcc792254f43bd2 to your computer and use it in GitHub Desktop.
Save brizzbane/cce5bbcc792254f43bd2 to your computer and use it in GitHub Desktop.
from Celery import Celery
app = Celery('tasks', broker='amqp://guest@localhost//', backend='amqp')
app.conf.CELERY_RESULT_BACKEND = 'amqp'
app.conf.CELERYD_LOG_COLOR = True
from elasticsearch_dsl import DocType
class Account(DocType):
class Meta:
index = 'accounts'
def save(self, ** kwargs):
return super(Account, self).save(** kwargs)
class BigProgram(object):
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.account_doc.in_use = False
self.account_doc.save()
def __init__(self,account_doc):
self.account_doc = account_doc
self.account_doc.in_use = True
self.account_doc.save()
self.account_doc = Account.get(id=self.account_doc._id) #exception on exit if this isn't here.
def do_something(self):
self.account_doc.some_value = 123
self.account_doc.save()
print 'something'
@app.task
def do_work(account_doc):
with BigProgram(account_doc) as bot:
bot.do_something()
if __name__ == '__main__':
search = Account.search()
response = search[0:10].execute()
for account_doc in response:
do_work.apply_async(account_doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment