Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/python3 bytes #65

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: python
python:
- "2.6"
- "2.7"
- "3.5"

env:
global:
Expand All @@ -15,7 +16,7 @@ install:
- make -C beanstalkd-1.10/
- mv beanstalkd-1.10/beanstalkd .
# Install Python dependencies.
- pip install -r .travis-requirements.txt --use-mirrors
- pip install -r .travis-requirements.txt

script: nosetests -c .nose.cfg

Expand Down
6 changes: 3 additions & 3 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ Here is a short example, to illustrate the flavor of beanstalkc:

>>> import beanstalkc
>>> beanstalk = beanstalkc.Connection(host='localhost', port=14711)
>>> beanstalk.put('hey!')
>>> beanstalk.put(b'hey!')
1
>>> job = beanstalk.reserve()
>>> job.body
'hey!'
>>> print(job.body)
b'hey!'
>>> job.delete()

For more information, see [the tutorial](TUTORIAL.mkd), which will explain most
Expand Down
88 changes: 44 additions & 44 deletions TUTORIAL.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ Basic Operation

Now that we have a connection set up, we can enqueue jobs:

>>> beanstalk.put('hey!')
>>> beanstalk.put(b'hey!')
1

Or we can request jobs:

>>> job = beanstalk.reserve()
>>> job.body
'hey!'
b'hey!'

Once we are done with processing a job, we have to mark it as done, otherwise
jobs are re-queued by beanstalkd after a "time to run" (120 seconds, per
Expand All @@ -61,23 +61,23 @@ receive a job. If such a `reserve` times out, it will return `None`:
If you use a timeout of 0, `reserve` will immediately return either a job or
`None`.

Note that beanstalkc requires job bodies to be strings, conversion to/from
strings is left up to you:
Note that beanstalkc requires job bodies to be bytes, conversion to/from
bytes is left up to you:

>>> beanstalk.put(42)
Traceback (most recent call last):
...
AssertionError: Job body must be a str instance
ValueError: Job body must be a bytes instance

There is no restriction on what characters you can put in a job body, so they
can be used to hold arbitrary binary data:

>>> _ = beanstalk.put('\x00\x01\xfe\xff')
>>> _ = beanstalk.put(b'\x00\x01\xfe\xff')
>>> job = beanstalk.reserve() ; print(repr(job.body)) ; job.delete()
'\x00\x01\xfe\xff'
b'\x00\x01\xfe\xff'

If you want to send images, just `put` the image data as a string. If you want
to send Unicode text, just use `unicode.encode` to convert it to a string with
If you want to send images, just `put` the image data as bytes. If you want
to send Unicode text, just use `str.encode` to convert it to bytes with
some encoding.


Expand All @@ -94,15 +94,15 @@ A beanstalkd client can choose one tube into which its job are put. This is the
tube "used" by the client. To see what tube you are currently using:

>>> beanstalk.using()
'default'
b'default'

Unless told otherwise, a client uses the 'default' tube. If you want to use a
different tube:

>>> beanstalk.use('foo')
'foo'
>>> beanstalk.use(b'foo')
b'foo'
>>> beanstalk.using()
'foo'
b'foo'

If you decide to use a tube, that does not yet exist, the tube is automatically
created by beanstalkd:
Expand All @@ -113,10 +113,10 @@ created by beanstalkd:
Of course, you can always switch back to the default tube. Tubes that don't have
any client using or watching, vanish automatically:

>>> beanstalk.use('default')
'default'
>>> beanstalk.use(b'default')
b'default'
>>> beanstalk.using()
'default'
b'default'
>>> beanstalk.tubes()
['default']

Expand All @@ -128,7 +128,7 @@ tubes are "watched" by the client. To see what tubes you are currently watching:

To watch an additional tube:

>>> beanstalk.watch('bar')
>>> beanstalk.watch(b'bar')
2
>>> beanstalk.watching()
['default', 'bar']
Expand All @@ -141,15 +141,15 @@ watching them:

To stop watching a tube:

>>> beanstalk.ignore('bar')
>>> beanstalk.ignore(b'bar')
1
>>> beanstalk.watching()
['default']

You can't watch zero tubes. So if you try to ignore the last tube you are
watching, this is silently ignored:

>>> beanstalk.ignore('default')
>>> beanstalk.ignore(b'default')
1
>>> beanstalk.watching()
['default']
Expand All @@ -173,7 +173,7 @@ Beanstalkd accumulates various statistics at the server, tube and job level.
Statistical details for a job can only be retrieved during the job's lifecycle.
So let's create another job:

>>> beanstalk.put('ho?')
>>> beanstalk.put(b'ho?')
3

>>> job = beanstalk.reserve()
Expand All @@ -200,7 +200,7 @@ If you try to access job stats after the job was deleted, you'll get a

Let's have a look at some numbers for the `'default'` tube:

>>> pprint(beanstalk.stats_tube('default')) # doctest: +ELLIPSIS
>>> pprint(beanstalk.stats_tube(b'default')) # doctest: +ELLIPSIS
{...
'current-jobs-ready': 0,
'current-jobs-reserved': 0,
Expand Down Expand Up @@ -268,15 +268,15 @@ Now let's have a practical look at those new possibilities. For a start, we can
create a job with a delay. Such a job will only be available for reservation
once this delay passes:

>>> beanstalk.put('yes!', delay=1)
>>> beanstalk.put(b'yes!', delay=1)
4

>>> beanstalk.reserve(timeout=0) is None
True

>>> job = beanstalk.reserve(timeout=1)
>>> job.body
'yes!'
b'yes!'

If we are not interested in a job anymore (e.g. after we failed to
process it), we can simply release the job back to the tube it came from:
Expand Down Expand Up @@ -332,7 +332,7 @@ around from our previous examples, so:

>>> job = beanstalk.peek(4)
>>> job.body
'yes!'
b'yes!'

Note that this peek did *not* reserve the job:

Expand All @@ -350,7 +350,7 @@ that would be returned by `reserve` -- the next ready job -- use `peek-ready`:

>>> job = beanstalk.peek_ready()
>>> job.body
'yes!'
b'yes!'

Note that you can't release, or bury a job that was not reserved by you. Those
requests on unreserved jobs are silently ignored:
Expand All @@ -370,14 +370,14 @@ You can, though, delete a job that was not reserved by you:

Finally, you can also peek into the special queues for jobs that are delayed:

>>> _ = beanstalk.put('o tempores', delay=120)
>>> _ = beanstalk.put(b'o tempores', delay=120)
>>> job = beanstalk.peek_delayed()
>>> job.stats()['state']
'delayed'

... or buried:

>>> _ = beanstalk.put('o mores!')
>>> _ = beanstalk.put(b'o mores!')
>>> job = beanstalk.reserve()
>>> job.bury()

Expand All @@ -391,13 +391,13 @@ Job Priorities

Without job priorities, beanstalkd operates as a FIFO queue:

>>> _ = beanstalk.put('1')
>>> _ = beanstalk.put('2')
>>> _ = beanstalk.put(b'1')
>>> _ = beanstalk.put(b'2')

>>> job = beanstalk.reserve() ; print(job.body) ; job.delete()
1
b'1'
>>> job = beanstalk.reserve() ; print(job.body) ; job.delete()
2
b'2'

If need arises, you can override this behaviour by giving different jobs
different priorities. There are three hard facts to know about job priorities:
Expand All @@ -413,16 +413,16 @@ different priorities. There are three hard facts to know about job priorities:

To create a job with a custom priority, use the keyword-argument `priority`:

>>> _ = beanstalk.put('foo', priority=42)
>>> _ = beanstalk.put('bar', priority=21)
>>> _ = beanstalk.put('qux', priority=21)
>>> _ = beanstalk.put(b'foo', priority=42)
>>> _ = beanstalk.put(b'bar', priority=21)
>>> _ = beanstalk.put(b'qux', priority=21)

>>> job = beanstalk.reserve() ; print(job.body) ; job.delete()
bar
b'bar'
>>> job = beanstalk.reserve() ; print(job.body) ; job.delete()
qux
b'qux'
>>> job = beanstalk.reserve() ; print(job.body) ; job.delete()
foo
b'foo'

Note how `'bar'` and `'qux'` left the queue before `'foo'`, even though they
were enqueued well after `'foo'`. Note also that within the same priority jobs
Expand Down Expand Up @@ -460,10 +460,10 @@ when creating the `Connection`:
... parse_yaml=False)

>>> beanstalk.tubes()
'---\n- default\n'
b'---\n- default\n'

>>> beanstalk.stats_tube('default') # doctest: +ELLIPSIS
'---\nname: default\ncurrent-jobs-urgent: 0\ncurrent-jobs-ready: 0\n...'
>>> beanstalk.stats_tube(b'default') # doctest: +ELLIPSIS
b'---\nname: default\ncurrent-jobs-urgent: 0\ncurrent-jobs-ready: 0\n...'

>>> beanstalk.close()

Expand All @@ -475,13 +475,13 @@ Alternatively, you can also pass a function to be used as YAML parser:

>>> beanstalk = beanstalkc.Connection(host='localhost',
... port=14711,
... parse_yaml=lambda x: x.split('\n'))
... parse_yaml=lambda x: x.split(b'\n'))

>>> beanstalk.tubes()
['---', '- default', '']
[b'---', b'- default', b'']

>>> beanstalk.stats_tube('default') # doctest: +ELLIPSIS
['---', 'name: default', 'current-jobs-urgent: 0', ...]
>>> beanstalk.stats_tube(b'default') # doctest: +ELLIPSIS
[b'---', b'name: default', b'current-jobs-urgent: 0', ...]

>>> beanstalk.close()

Expand Down
Loading