Private
Public Access
1
0
Files
rowsandall/rowers/longtask.py
2017-11-01 00:16:05 +01:00

62 lines
1.6 KiB
Python

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