Private
Public Access
1
0

task progress monitoring celery works on client

This commit is contained in:
Sander Roosendaal
2017-11-01 00:16:05 +01:00
parent 5525dad1f6
commit fb85e5f53b
4 changed files with 100 additions and 8 deletions

61
rowers/longtask.py Normal file
View File

@@ -0,0 +1,61 @@
import numpy as np
import time
from redis import StrictRedis,Redis
from celery import result as celery_result
import json
redis_connection = StrictRedis()
import redis
import threading
class Listener(threading.Thread):
def __init__(self, r, channels):
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.subscribe(channels)
def work(self, item):
print item['channel'], ":", item['data']
def run(self):
for item in self.pubsub.listen():
if item['data'] == "KILL":
self.pubsub.unsubscribe()
print self, "unsubscribed and finished"
break
else:
self.work(item)
def longtask(aantal,jobid=None,debug=False):
if jobid:
if debug:
job = celery_result.AsyncResult(jobid)
else:
job = Job.fetch(jobid,connection=redis_connection)
counter = 0
channel = 'tasks'
for i in range(aantal):
time.sleep(1)
counter += 1
if counter > 10:
counter = 0
if debug:
progress = 100.*i/aantal
print progress
if jobid != None:
redis_connection.publish(channel,json.dumps(
{
'done':i,
'total':aantal,
'id':jobid,
}
))
redis_connection.publish(channel,'KILL')
return 1