websucker-pip/websucker/webqueue.py

36 lines
930 B
Python
Raw Permalink Normal View History

2020-05-07 14:09:45 +00:00
import greenstalk
import random
MAX_PRIORITY = 0
MIN_PRIORITY = 4000000000
MAX_FLOAT_PRIORITY = 10000.0
def map_priority(p,max_priority):
p = p / max_priority
return MIN_PRIORITY - (p*MIN_PRIORITY)
class BeanstalkdQueue:
def __init__(self,host,port,tube):
self.c = greenstalk.Client(host,port,use=tube,encoding="utf8")
def queue_priority_domains(self,priority_domains):
for domain,priority in priority_domains:
p = priority / MAX_FLOAT_PRIORITY
p = MIN_PRIORITY - (p*MIN_PRIORITY)
self.c.put(domain,p)
def queue_random_domains(self,domains):
for domain in domains:
p = random.randint(MAX_PRIORITY,MIN_PRIORITY)
self.c.put(domain,p)
def consume_domains(self,callback):
while True:
job = self.c.reserve()
domain = job.body
self.c.delete(job)
callback(domain)