python3的已安装绑定
pystalkd的Python项目详细描述
Beanstalk is a simple, fast work queue. Its interface is generic, but was originally designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously
http://kr.github.io/beanstalkd/
pystalkd是一种针对python3的豆茎绑定。这个图书馆是 基于https://github.com/earl/beanstalkc,应该是api 兼容的。
pip install pystalkd
或来源:
python setup.py install
使用
>>>frompystalkd.BeanstalkdimportConnection>>>c=Connection("localhost",11300)#if no argument is given default configuration is used>>>c.put("hey!")>>>job=c.reserve(0)>>>job.body"hey!"
其中一个目标是使api与beanstalkc兼容,因此 教程应有效: https://github.com/earl/beanstalkc/blob/master/TUTORIAL.mkd
API方面的主要区别是:
- 在预期的秒数中,pystaldd也接受一个timedelta 对象
- 您可以使用with关键字临时观看和使用一个管道
print(c.using())# "default"withc.temporary_use("test"):print(c.using())# "test"print(c.using())# "default"print(c.watching())# ["default"]withc.temporary_use("test"):print(c.watching())# ["default", "test"]print(c.watching())# ["default"]
- 您还可以访问“bytes”api。保持兼容性 对于beanstalkc,api只处理字符串,但是现在可以使用 以“_字节”结尾的函数(内部受控制 使用rawparamater)直接处理字节
fromosimporturandomtest_bytes=urandom(50)job_id=c.put_bytes(test_bytes)job=c.reserve_bytes(0)print(job.body)# b'i\x91\xdf\xf8\x1b?zj....'job_id2=c.put("string")job2=c.reserve_bytes(0)print(job2.body)# b'string'
注意:可以将reserve_bytes与put一起使用,并获取原始字符串 (未编码),但另一种方法可能会导致问题
测试
使用默认主机和端口(localhost,11300)进行测试:
python3 test.py
在特定主机上测试(如果未指定端口,则使用11300)
python3 test.py host [port]