如何使用 App Engine 中的任务队列 Python API 传递压缩数据?

发布于 2024-08-19 09:32:10 字数 446 浏览 5 评论 0原文

我尝试在任务队列中的任务中使用压缩数据,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒一口袋星星 2024-08-26 09:32:10

不要使用参数,而是使用有效负载,其中包含请求正文中的未编码数据。然后您可以使用 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.

伴随着你 2024-08-26 09:32:10

阅读文档...(我强调!):

params 要使用的参数字典
对于这个任务。值在
字典可能是可迭代的以指示
重复的参数。可能不是
为 POST 请求指定,如果
有效负载已指定。对于邮政
请求时,这些参数将被编码
作为“应用程序/x-www-form-urlencoded”
并设置为有效负载;对于所有其他
方法,参数将是
转换为查询字符串。不得
如果 URL 已存在,则指定
包含查询字符串和方法
是 GET。

zlib.compress 生成任意字节字符串...但查询字符串转换会将其解释为 Unicode!因此,使用任何 1 字节编解码器(例如 latin-1)对压缩结果进行 .encode 以便传递(实际上是二进制的)参数字节串,并且.decode 使用相同的编解码器从“unicode”字符串返回到可以解压缩的字节字符串。唷...您确定压缩对于您的应用程序的性能至关重要,值得进行这组奇怪的旋转,或者避免它不是更好吗?-)

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?-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文