John John - 11 months ago 66
PHP Question

PHP / Python Beanstalk sockets event Job

I'm using Phalcon php server side and I send job to beanstalk as the doc said.

To execute all jobs inside beanstalk I have a python script. This script search if there is existing jobs and execute them. But for the moment I execute this script with linux command line.

Then, I want to execute my python script automatically when a job was created and execute the task. I thought about sockets.

It's possible to catch the beanstalk socket event when it receives a job and execute my python script ?

Answer Source

Finally I wrote a python script with Tornado Beanstalkt.

Here is my code.

import tornado
import beanstalkt

def show(msg, value, cb):
  print(msg % value)

def stop():

def connect(s):
   print('Connection established')

def reserve():
  client.reserve(callback=lambda s: show(
      "Reserved job %s", s, lambda: delete(s["id"])))

def delete(job_id):
  client.delete(job_id, callback=lambda s: show(
      "Deleted job with id %d", job_id, reserve))

ioloop = tornado.ioloop.IOLoop.instance()
client = beanstalkt.Client(host='my-server', port=11300)

Theses line will establish the connection to my beanstalk server.


I'll start the worker once the connect is successful that is why I'm using a callback inside the connect method. The watch method focus on the tube that I want and start method will start the async loop non blocking I/O.

In my connect function I'll call reserve function. If no job is available to be reserved and no timeout is given then reserve will wait for a futur job and will be triggered with a beanstalk socket.

After that I can give what I want inside the reserve function. Here I send the show function for test my process. After my print I'll call the delete callback to delete the current job.

Hope it helps.