簡體   English   中英

具有Memcache的GAE上的Python錯誤-TypeError:無法散列的類型:“列表”

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM