【问题标题】:Threading with web.py and memcached使用 web.py 和 memcached 进行线程化
【发布时间】:2014-05-17 13:22:32
【问题描述】:

我使用 memchached 和 web.py 来存储会话(由于速度和可扩展性问题,我不希望它们存储在磁盘上)。我正在使用来自 here 的代码和 pylibmc。

但是,它一直在中断。我认为可能是多个线程破坏了它。它肯定 memcached 使用磁盘存储进行会话工作正常,但使用自定义存储,它只是挂起。

这里的问题可能是线程吗?如何解决这个问题?

【问题讨论】:

    标签: python multithreading memcached web.py


    【解决方案1】:

    通过为每个请求创建一个新的 mc 对象来解决问题。因此MemCacheStore 看起来像这样:

    class MemCacheStore(web.session.Store):
                    ...
        def __contains__(self, key):
            mc = memcache.Client(['127.0.0.1:11211'], debug=0)
            return mc.get(key) != None
        def __getitem__(self, key):
            mc = memcache.Client(['127.0.0.1:11211'], debug=0)
            return mc.get(key)
                   ...
    

    【讨论】:

      猜你喜欢
      • 2010-12-09
      • 2010-09-25
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      相关资源
      最近更新 更多