如何使用 App Engine 中的任务队列 Python API 传递压缩数据?
我尝试在任务队列中的任务中使用压缩数据,如下所示:
t = taskqueue.Task(url='/tasks/queue',
params={'param': zlib.compress(some_string)}
但是,当我尝试在队列处理程序中解压缩它时,如下所示,
message = self.request.get('param')
message = zlib.decompress(message)
我收到此错误:
UnicodeEncodeError:“ascii”编解码器无法对位置 2 中的字符 u'\u06b8' 进行编码:序数不在范围内(128)
有人知道这里发生了什么吗?有解决办法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用参数,而是使用有效负载,其中包含请求正文中的未编码数据。然后您可以使用 zlib.decompress(self.request.body) 来检索数据。
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.阅读文档...(我强调!):
zlib.compress
生成任意字节字符串...但查询字符串转换会将其解释为 Unicode!因此,使用任何 1 字节编解码器(例如latin-1
)对压缩结果进行.encode
以便传递(实际上是二进制的)参数字节串,并且.decode
使用相同的编解码器从“unicode”字符串返回到可以解压缩
的字节字符串。唷...您确定压缩对于您的应用程序的性能至关重要,值得进行这组奇怪的旋转,或者避免它不是更好吗?-)Read the docs... (my emphasis!):
zlib.compress
produces an arbitrary string of bytes... but then query-string conversion interprets it as Unicode! So, use any 1-byte codec, such aslatin-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 candecompress
. 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?-)