标签:touch message int uri 自己的 kick 计算 ast priority
put reserve delete -----> [READY] ---------> [RESERVED] --------> *poof*
#!/usr/bin/env python import beanstalkc beanstalk=beanstalkc.Connection(host="127.0.0.1",port=11300) # pruducer beanstalk.put(‘hello‘) beanstalk.put(‘world‘) # consumer job1=beanstalk.reserve() print "job1: " + job1.body job1.delete() job2=beanstalk.reserve(timeout=1) print "job2: " + job2.body job2.delete() job3=beanstalk.reserve(timeout=1) # Error,队列中已经没有job了 print "job3: " + job3.body job3.delete()
print beanstalk.tubes() # default print beanstalk.using() # default beanstalk.use(‘queue1‘) print beanstalk.using() # queue1 beanstalk.use(‘queue2‘) # queue2 print beanstalk.using() print beanstalk.tubes() # default, queue2
如上面的例子,在最后的tubes()命令打印所有tube的时候,并没有看到queue1,这是因为没有任何client 在using或者watching的tube会自动消失。
可以使用watch命令让client同时处理多个tube,而不用担心tube会被销毁:
print beanstalk.tubes() # default print beanstalk.using() # default beanstalk.use(‘queue1‘) beanstalk.watch(‘queue1‘) print beanstalk.using() # queue1 beanstalk.use(‘queue2‘) # queue2 print beanstalk.using() print beanstalk.tubes() # default, queue1, queue2 print beanstalk.watching() # default, queue1
watch的tube如果不存在,会被自动创建,可以用ignore命令取消关注tube:
beanstalk.watch(‘queue3‘) print beanstalk.watching() # default, queue3 beanstalk.ignore(‘queue3‘) print beanstalk.watching() # default
put with delay release with delay ----------------> [DELAYED] <------------. | | | (time passes) | | | put v reserve | delete -----------------> [READY] ---------> [RESERVED] --------> *poof* ^ ^ | | | \ release | | | `-------------‘ | | | | kick | | | | bury | [BURIED] <---------------‘ | | delete `--------> *poof*
# pruducer beanstalk.put(‘hello‘, delay=10) beanstalk.put(‘world‘) # consumer job1=beanstalk.reserve() print "job1: " + job1.body # World job1.delete() job2=beanstalk.reserve(timeout=0) print "job2: " + job2.body # Error,
put命令也支持优先级参数:
# pruducer beanstalk.put(‘hello‘, priority=10) beanstalk.put(‘world‘, priority=9) # consumer job1=beanstalk.reserve() print "job1: " + job1.body # world job1.delete() job2=beanstalk.reserve(timeout=0) print "job2: " + job2.body # hello job2.delete()
release命令可以释放job回队列:
# pruducer beanstalk.put(‘hello‘) beanstalk.put(‘world‘) # consumer job1=beanstalk.reserve() print "job1: " + job1.body # hello job1.release() job2=beanstalk.reserve(timeout=0) print "job2: " + job2.body # hello
bury命令将job放到一个特殊的FIFO队列中,之后不能被reserve命令获取,但可以用kick命令扔回工作队列中,之后就能被消费了:
# pruducer beanstalk.put(‘hello‘) beanstalk.put(‘world‘) # consumer job1=beanstalk.reserve(timeout=0) job1.bury() print job1.stats()[‘state‘] # buried job2=beanstalk.reserve(timeout=0) print "job2: " + job2.body # world job2.delete() beanstalk.kick() job3=beanstalk.reserve(timeout=0) print "job3: " + job3.body # hello job3.delete()
peek命令允许查看一个job,但不会reserve它;
# pruducer beanstalk.put(‘hello‘) beanstalk.put(‘world‘) #print(beanstalk.stats()) # consumer job1=beanstalk.reserve(timeout=0) job1_id=job1.stats()[‘id‘] print job1_id job1_r=beanstalk.peek(job1_id) print "job1 " + job1_r.body # hello job2=beanstalk.reserve(timeout=0) print "job2: " + job2.body # hello job2.delete() job3=beanstalk.reserve(timeout=0) print "job3: " + job3.body # world job3.delete()
标签:touch message int uri 自己的 kick 计算 ast priority
原文地址:http://www.cnblogs.com/chenny7/p/7244913.html