[英]Python error on GAE with Memcache - TypeError: unhashable type: 'list'
我有一个小型的Python应用程序,它可以生成表单,用户输入一些字符串,然后将它们收集为数组,然后将该数组添加(或尝试)作为Google Memcache中键的值。
这是脚本:
import webapp2
from google.appengine.api import memcache
MAIN_PAGE_HTML = """\
<html>
<body>
<form action="/get" method="post">
<div><input name="ID"/></div>
<div><input name="param1"/></div>
<div><input name="param2"/></div>
<div><input name="param3"/></div>
<div><input name="param4"/></div>
<div><input name="param5"/></div>
<div><input type="submit" value="Change Status"></div>
</form>
</body>
</html>
"""
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.write(MAIN_PAGE_HTML)
class status(webapp2.RequestHandler):
def post(self):
paramarray= (self.request.get_all('param1'),
self.request.get_all('param2'),
self.request.get_all('param3'),
self.request.get_all('param4'),
self.request.get_all('param5'))
array1 = tuple(paramarray)
memcache.add(key=(self.request.get_all('ID')), value=array1, time=3600)
app = webapp2.WSGIApplication([
('/', MainPage),
('/get', status),
], debug=True)
我尝试将paramarray
设置为元组,而不是列表。 仍然出现相同的错误:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~gtech-iot/1.391041688070473184/main.py", line 41, in post
memcache.add(key=(self.request.get_all('ID')), value=array1, time=3600)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/memcache/__init__.py", line 785, in add
namespace=namespace)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/memcache/__init__.py", line 868, in _set_with_policy
rpc = self._set_multi_async_with_policy(policy, {key: value},
TypeError: unhashable type: 'list'
尝试设置带有或不带有array1=..
语句的大括号,规则,正方形的花括号
请帮忙,谢谢
您正在传递一个list
-由self.request.get_all('ID')
返回,作为memcache.add()
的key
参数,这不行。
我建议对键进行完整性检查-您希望它至少是一个非空字符串,以使其在内存缓存键中有意义。
如果确实在请求中有多个可能的“ ID”值,则可以将它们串联在一个字符串中(我仍然会质疑可用性,因为不能保证在下一个请求中它们将以相同的顺序返回),如果没有,那么也许请改用self.request.get('ID')
-它不会返回类似self.request.get_all('ID')
的列表。
问题不在于存储在Memcache中的值(array1),而是在于键。 您不能将类型列表的值用作键。 检查Python Memcache API 文档 。
任何带有'key'参数的方法都可以将该键作为字符串(是否为unicode)或(hash_value,string)元组接受,其中通常用于分片到memcache实例上的hash_value会被忽略,例如Google App引擎透明地处理分片。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.