kombu icon indicating copy to clipboard operation
kombu copied to clipboard

Python 3 bytes vs. str issue when using redis

Open dnene opened this issue 13 years ago • 6 comments

I have been attempting just the basic get started flow as documented on the celery site, under Python 3. I run into this issue where the redis client returns bytes, but the code treats them as strings and attempts to perform string operations on them which result in an exception.

examples :

In Channel._brpop_read(..)

I had to change

dest = dest.rsplit(self.sep, 1)[0]

to

dest = dest.decode("utf-8").rsplit(self.sep, 1)[0] item = item.decode("utf-8")

In the above scenario both dest and items are bytes as returned by the redis client. The rsplit is available only on a str. Moreover if I don't convert item to str, I get a failure further down

Similarly in Channel.get_table(..)

I had to change

return [tuple(val.split(self.sep)) for val in values]

to

return [tuple(val.decode("utf-8").split(self.sep)) for val in values]

since values is a collection of bytes and not strings.

I would've attempted to offer a pull request, but wasn't quite sure a) if there are many other places similar code changes would be required and b) if it was in some way dependent on the data being pushed into redis (and thus the issue was with how the data was being pushed into redis not the expected output types as I did in this case).

The worker thread was pretty much what was given in the getting started docs but reproducing the same below :

from celery import Celery

celery = Celery('tasks',backend="redis://localhost:6379/0", broker="redis://localhost:6379/0")

@celery.task def add(x, y): return x + y

The client dispatching the jobs was

from tasks import add import time

future = add.delay(3,11) print(future) print(future.ready()) time.sleep(2) print(future.ready()) print(future.get())

The python version was "Python 3.2.3 (default, May 3 2012, 15:51:42) [GCC 4.6.3] on linux2"

Installed packages were : redis==2.7.2 kombu==2.5.3 amqp==1.2.0 anyjson==0.3.3 billiard==2.7.3.19 celery==3.0.12 celery-with-redis==3.0 distribute==0.6.27 mock==1.0.1 nose==1.2.1 nose-cover3==0.1.0 python-dateutil==2.1 pytz==2012h six==1.2.0

dnene avatar Dec 06 '12 12:12 dnene

Have been waiting for a response. I would gladly offer a pull request. But for this one issue I stated above :

I would've attempted to offer a pull request, but wasn't quite sure a) if there are many other places similar code changes would be required and b) if it was in some way dependent on the data being pushed into redis (and thus the issue was with how the data was being pushed into redis not the expected output types as I did in this case).

So given the lack of response .. would you prefer

a) I offer a pull request based on the above changes ? b) I fork and maintain an independent version ? c) Something else?

dnene avatar Jan 29 '13 06:01 dnene

I'm sorry, I've been incredibly busy lately so I have had to focus on Python 2 issues. There's so much mail that messages just disappear in my mailbox, hopefully I will be able to manage this at some point :)

I'm not sure but maybe this can be fixed at a lower level so that it doesn't have to convert it in so many places? Otherwise it looks good, you can use kombu.utils.encoding.bytes_to_str, this way it will work on both Py2 and Py3.

Option A is definitely preferred

ask avatar Apr 17 '13 13:04 ask

I've encountered this issue, as well. I've since forked and am working on a pull request to fix it. I hope to have it done soon. Thanks!

queertypes avatar Apr 17 '13 20:04 queertypes

I have a set of changes I have been working with privately. I just forked both kombu and celery to userid dnene. Should apply the changes there through the day today. There have been one or two more issues than the one I reported above and will update the same as well.

dnene avatar Apr 18 '13 03:04 dnene

Issued a pull request https://github.com/celery/kombu/pull/225

The pull request contains the fix for one more error under python 3 (faced when running python setup.py install)

Also separately created issue https://github.com/celery/celery/issues/1311 . Did not file a pull request for the same since I have very little idea how/where to fix it.

Some other changes I had to do locally are now no longer required with the current HEAD.

Collectively these issues are all the issues I was facing with Python 3.3 with kombu and celery.

dnene avatar Apr 18 '13 06:04 dnene

Closed by pull request #225. Thanks!

ask avatar Apr 18 '13 09:04 ask