English 中文(简体)
How do I pass compressed data using the Task Queue Python API in App Engine?
原标题:

I m trying to use compressed data with my Tasks in the Task Queue like so:

t = taskqueue.Task(url= /tasks/queue ,
                   params={ param : zlib.compress(some_string)}

However when I try to decompress it in the queue handler like so

message = self.request.get( param )
message = zlib.decompress(message)

I get this error:

UnicodeEncodeError: ascii codec can t encode character u u06b8 in position 2: ordinal not in range(128)

Anyone know of what s going on here? Is there a work around?

最佳回答

Instead of using params, use payload, which includes your data in the body of the request, unencoded. Then you can use zlib.decompress(self.request.body) to retrieve the data.

问题回答

Read the docs... (my emphasis!):

params Dictionary of parameters to use for this Task. Values in the dictionary may be iterable to indicate repeated parameters. May not be specified for a POST request if payload is already specified. For POST requests, these params will be encoded as application/x-www-form-urlencoded and set to the payload; for all other methods, the parameters will be converted to a query string. May not be specified if the URL already contains a query string and the method is GET.

zlib.compress produces an arbitrary string of bytes... but then query-string conversion interprets it as Unicode! So, use any 1-byte codec, such as latin-1, to .encode the compressed results in order to pass (what s actually a binary) bytestring of params, and the same codec for a .decode to get back from the "unicode" string to a string of bytes that you can decompress. Phew... you sure the compression is crucial enough to your app s performance to be worth this weird set of gyrations, or wouldn t it be better to eschew it?-)





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签