简体   繁体   中英

How to tell a beanstalkc receiver to wait X seconds before reserving a task?

I have 2 beanstalkc receivers watching the same tube "tubename" .

I would like one beanstalkc receiver to have priority over the other. In order to achieve this, I would like to tell the lowest-priority beanstalkc receiver to wait for task being X seconds old before reserving them.

I found "reserve-with-timeout" , but I neither really understand it nor do I managed to make it work successfully for my use case.

class MyBeanstalkReceiver():
    def __init__(self, host=beanstalkc.DEFAULT_HOST, port=beanstalkc.DEFAULT_PORT,
                 tube="default", timeout=1):
        self.tube = tube
        self.host = host
        self.port = port
        self.timeout = timeout

    def run(self):
        while True:
            self.run_once()

    def run_once(self):
        job = self._get_task()

        try:
            body = job.body
            data = json.loads(body)
            self.job(data)
        except Exception as e:
            job.delete()

    def job(self, data):
        print(data)

    def beanstalk(self):
        beanstalk = beanstalkc.Connection(host=self.host, port=self.port)
        beanstalk.use(self.tube)
        beanstalk.watch(self.tube)
        return beanstalk

    def _get_task(self):
        return self.beanstalk().reserve(self.timeout)

And my 2 beanstalkc receivers:

# receiver 1
w = MyBeanstalkReceiver(hosts=["localhost:14711"], tube="tubename", timeout=1)
w.run()
# receiver 2
w = MyBeanstalkReceiver(hosts=["localhost:14711"], tube="tubename", timeout=10000)
w.run()

Between the 2 receivers, with a timeout of 1 and 10000, nothing changes when I send tasks over the tube: both end up managing the same quantity of tasks put inside the tube "tubename" .

Any idea on how to proceed to make "receiver 1" prioritary over "receiver 2"?

The timeout in reserve is for how long the client will wait before returning without a job.

You may be looking for put (with a delay) , where the job is not released until it has been in the queue for at least n seconds.

There is also a priority per job. If the receiver could have seen them both at the same time, it will return any jobs with a higher priority (ie: closer to 0) rather than with a lower priority (with a larger number).

Beanstalkd does not differentiate between priorities of the clients or receivers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM