python - Pyramid 烧杯缓存问题 - TypeError : int() argument must be a string or a number, 而不是 'NoneType'

标签 python pyramid beaker

.ini 文件

cache.regions = default_term, second, short_term, long_term
cache.type = memcached
cache.url = 127.0.0.1:11211
cache.second.expire = 1
cache.short_term.expire = 60
cache.default_term.expire = 300
cache.long_term.expire = 3600

__init__.py 文件

from pyramid_beaker import set_cache_regions_from_settings
def main(global_config, **settings):
set_cache_regions_from_settings(settings)
...

测试.py文件

from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options

cache_opts = {
'cache.data_dir': '/tmp/cache/data',
'cache.lock_dir': '/tmp/cache/lock',
'cache.regions': 'short_term, long_term',
'cache.short_term.type': 'ext:memcached',
'cache.short_term.url': '127.0.0.1.11211',
'cache.short_term.expire': '3600',
'cache.long_term.type': 'file',
'cache.long_term.expire': '86400',
}

cache = CacheManager(**parse_cache_config_options(cache_opts))

@cache.region('short_term', 'test')
def test_method(*args, **kwargs):

执行上面的代码会出现错误:

...
File "c:\python27\lib\site-packages\python_memcached-1.48-py2.7.egg\memcache.py", line      1058, in __init__
self.port = int(hostData.get('port', 11211))
TypeError: int() argument must be a string or a number, not 'NoneType'

知道什么可能导致错误/或者我遗漏了任何东西吗?

最佳答案

看看你的测试配置,url 设置有错误:

'cache.short_term.url': '127.0.0.1.11211',

请注意,其中没有 : 冒号。您使用的 memcached 模块使用正则表达式来尝试解析该值,并且当您将该值指定为主机时,该方法将 port 设置为 None:

>>> host = '127.0.0.1.11211'
>>> re.match(r'^(?P<host>[^:]+)(:(?P<port>[0-9]+))?$', host).groupdict()
{'host': '127.0.0.1.11211', 'port': None}

这是你的回溯的来源。将 cache_opts 字典更改为:

'cache.short_term.url': '127.0.0.1:11211',

一切都会正常进行:

>>> host = '127.0.0.1:11211'
>>> re.match(r'^(?P<host>[^:]+)(:(?P<port>[0-9]+))?$', host).groupdict()
{'host': '127.0.0.1', 'port': '11211'}

关于python - Pyramid 烧杯缓存问题 - TypeError : int() argument must be a string or a number, 而不是 'NoneType',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12177541/

相关文章:

python - 如何从 ViewSet 类创建 Django REST Framework View 实例?

python - 在 Windows 上安装 PyGIMP

python - Bottle.py 与 Beaker 的 session

python - 有没有一种方法可以将数据从表单映射到插入数据库而无需显式定义每个变量?

python - Pyramid 安全性 - 为未经身份验证的用户添加主体

django - 在 Google App Engine (Django) 上使用 Beaker

python - 通过 Google App Engine 上的 Bottle 启用 Beaker SessionMiddleware

python - 将字典的字符串表示形式转换为字典

python - 在 Python 中随机生成特定长度的整数分区的算法?

python - Pyramid 项目的 .jinja 模板中的 Vue.js