【问题标题】:Connect to python-memcached using memcached.sock path使用 memcached.sock 路径连接到 python-memcached
【发布时间】:2018-03-24 18:16:09
【问题描述】:

如何使用 memcached.sock 的路径连接到 Python-memcached? (Python 2.7)

Memcached 预装在我的主机 (Webfaction) 上。我已经启动它并验证它正在运行。我还验证了 memcached.sock 在我的主目录中。文档说:

“一旦你的 Memcached 实例启动并运行,你可以通过 带有软件的套接字文件 (~/memcached.sock) 的路径或 使用 memcached 的库。”

我试过这个:

import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=1)
mc.set("some_key", "Some value")

但我在 mc.set 上遇到错误:

Connection refused.  Marking dead.

我也试过

mc = memcache.Client('~/memcached.sock', debug=1)

那么mc.set上的错误是

Name or service not known.  Marking dead.

【问题讨论】:

    标签: python memcached python-memcached


    【解决方案1】:

    我这样做了:

    设置:

    memcached -d -s /tmp/memcached.sock
    

    代码:

    import memcache
    mc = memcache.Client(['unix:/tmp/memcached.sock'], debug=0)
    mc.set('hello', 'world')
    print(mc.get('hello'))
    

    完整测试:

    docker run --rm -t python:3.6 bash -c "$(cat << 'EOF'
    # setup
    apt-get update && \
    apt-get install -y memcached && \
    
    # start memcached
    memcached -u nobody -d -s /tmp/memcached.sock && \
    
    # install the requirements
    pip install python-memcached && \
    
    # run the code
    python <(cat << FOE
    import memcache
    mc = memcache.Client(['unix:/tmp/memcached.sock'], debug=0)
    mc.set('hello', 'world')
    print(mc.get('hello'))
    FOE
    )
    
    EOF
    )"
    

    ...剧透,它打印出"world"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 2014-01-27
      相关资源
      最近更新 更多