PHP / Python Beanstalk sockets event Job -
i'm using phalcon php server side , send job beanstalk doc said.
to execute jobs inside beanstalk have python script. script search if there existing jobs , execute them. moment execute script linux command line.
then, want execute python script automatically when job created , execute task. thought sockets.
it's possible catch beanstalk socket event when receives job , execute python script ?
finally wrote python script tornado beanstalkt.
here code.
import tornado import beanstalkt def show(msg, value, cb): print(msg % value) cb() def stop(): client.close(ioloop.stop) def connect(s): print('connection established') reserve() 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 id %d", job_id, reserve)) ioloop = tornado.ioloop.ioloop.instance() client = beanstalkt.client(host='my-server', port=11300) client.connect(callback=connect) client.watch("my-tube") ioloop.start()
theses line establish connection beanstalk server.
client.connect(callback=connect) client.watch("my-tube") ioloop.start()
i'll start worker once connect successful why i'm using callback inside connect
method. watch
method focus on tube want , start
method start async loop non blocking i/o.
in connect
function i'll call reserve
function. if no job available reserved , no timeout given reserve
wait futur job , triggered beanstalk socket.
after can give want inside reserve
function. here send show function test process. after print i'll call delete callback delete current job.
hope helps.
Comments
Post a Comment