【问题标题】:Gunicorn isnt finding 'memcache' module but it is found when I run python3 manage.py runserver 0.0.0.0:8000Gunicorn 没有找到“memcache”模块,但是当我运行 python3 manage.py runserver 0.0.0.0:8000 时找到了它
【发布时间】:2020-06-04 23:56:07
【问题描述】:

我的服务器出现问题。这是一个多租户项目,Windows 服务器中的项目工作正常,但 Ubuntu 中的项目虽然已安装,但给我一个“没有名为‘memcache’的模块”错误。我知道它已安装,因为我运行了“python3 manage.py runserver 0.0.0.0:8001”,当我通过个人浏览器访问时工作正常。Gunicorn 正确指向我的虚拟环境,当我重新启动时没有日志错误服务,我现在很绝望。

我的配置:

CACHE_HOST = os.getenv('cache_host', '127.0.0.1')

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': f'{CACHE_HOST}:11211',
    },
    'estadisticos': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': f'{CACHE_HOST}:11211',
    }
}

【问题讨论】:

    标签: python django memcached gunicorn


    【解决方案1】:

    那么您的内存缓存主机是否在 localhost 上运行?如果不是,gunicorn 可能是作为服务启动的,并且没有正确的环境变量 cache_host

    无论如何,我建议您在设置文件的末尾添加打印 并打印一张以查看您是否对 gunicorn 和命令行使用相同的 ptyhon:

    import sys  # if not already imported in settings
    print("my python is ", sys.executable)
    print("CACHES", CACHES, file=sys.stderr)
    

    或者如果您看不到标准输出: 使用 open("/tmp/mylog.log") 作为 fout: print("我的 python 是", sys.executable, file=fout) 打印(“缓存”,缓存,文件=fout)

    
    Check that python3 manage.py runserver 0.0.0.0:8001 creates the same trace as running gunicorn.
    delete the file `/tmp/mylog.log` between the two runs.
    

    如果输出相同,但 memcached 只适用于其中一个而不适用于另一个,那么您必须检查 django 设置是否在其他地方被覆盖

    【讨论】:

    • 相同的输出而不是覆盖的设置:/,我在 Windows 服务器上运行的第二个租户没有问题,我开始问这是否是 gunicorn 问题,因为无法识别 python-memcached,我'得到与我执行 pip uninstall python-memcached 一样的输出
    • 改变了答案。请同时检查sys.executable 的输出,也许你有不同的 virtualenvs / python 环境或类似的东西
    • 你刚刚救了我一个晚上,因为我明天早上要和客户开会!非常感谢
    猜你喜欢
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 2011-05-19
    • 2018-01-23
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多