【问题标题】:a bytes-like object is required, not 'str' - error需要一个类似字节的对象,而不是 'str' - 错误
【发布时间】:2016-02-07 18:22:48
【问题描述】:

我有 python-memcached (1.57) 和 django-celery (3.1.17)、celery (3.1.20) 和 python 3.5。当我尝试实现 http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html#ensuring-a-task-is-only-executed-one-at-a-time

时,我总是收到以下错误
Task tasks.live_task[a2ed1faf-0fce-4855-a206-40f2fbdae1a8] raised unexpected: TypeError("a bytes-like object is required, not 'str'",)
Traceback (most recent call last):
File "/app/current/venv/lib/python3.5/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/app/current/venv/lib/python3.5/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/app/current/src/helps.py", line 62, in wrapper
if acquire():
File "/app/current/src/helps.py", line 57, in acquire
return cache.add(id, "true", time_out)
File "/app/current/venv/lib/python3.5/site-packages/django/core/cache/backends/memcached.py", line 80, in add
return self._cache.add(key, value, self.get_backend_timeout(timeout))
File "/app/current/venv/lib/python3.5/site-packages/memcache.py", line 633, in add
return self._set("add", key, val, time, min_compress_len, noreply)
File "/app/current/venv/lib/python3.5/site-packages/memcache.py", line 983, in _set
server, key = self._get_server(key)
File "/app/dashboard/current/venv/lib/python3.5/site-packages/memcache.py", line 413, in _get_server
serverhash = serverHashFunction(str(serverhash) + str(i))
File "/app/current/venv/lib/python3.5/site-packages/memcache.py", line 65, in cmemcache_hash
(((binascii.crc32(key) & 0xffffffff)
TypeError: a bytes-like object is required, not 'str'

【问题讨论】:

  • 该错误很可能与您的python版本有关。尝试使用bytes(string, encoding="UTF-8") 转换为字节。
  • 尝试使用python3-memcached
  • @DerekKwok python3-memcached>=1.51 还是锚定到 1.51 特别重要?
  • @tdelaney 我已经删除了版本号,没有特别的理由只使用版本 1.51-

标签: python django memcached celery python-memcached


【解决方案1】:

python 3.5 不支持python-memchached 如果你用过python-memchached,下面的命令会对你有所帮助。

pip uninstall python-memcached
pip install python3-memcached

【讨论】:

  • 其实这是个陷阱。 python3-memcached 确实有效,但已停产,官方的 python-memcached 仅适用于 utf-8 编码数据。据说有一个修复它但对我来说它仍然爆炸:github.com/linsomniac/python-memcached/issues/80
  • 没错。 python3-memcached 已弃用。它只是 python-memcached 的 py3 兼容端口,此后添加了 py23。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
相关资源
最近更新 更多