php memcache 连接池,带有Python连接池的Memcache客户端?

很多人感谢@Heikki Toivenen为这个问题提供了一些想法!但是,我不知道如何准确地调用get()方法以便在PoolClient中使用memcache客户机。使用任意名称直接调用get()方法会导致AttributeError或MemcachedKeyNoneError。在

通过结合@Heikki Toivonen和pylibmc的问题解决方案,我提出了以下针对该问题的代码,并发布在这里,以方便将来的用户(我已经调试了此代码,它应该可以运行了):import Queue, memcache

from contextlib import contextmanager

memcache.Client = type('Client', (object,), dict(memcache.Client.__dict__))

# Client.__init__ references local, so need to replace that, too

class Local(object): pass

memcache.local = Local

class PoolClient(object):

'''Pool of memcache clients that has the same API as memcache.Client'''

def __init__(self, pool_size, pool_timeout, *args, **kwargs):

self.pool_timeout = pool_timeout

self.queue = Queue.Queue()

for _i in range(pool_size):

self.queue.put(memcache.Client(*args, **kwargs))

print "pool_size:", pool_size, ", Queue_size:", self.queue.qsize()

@contextmanager

def reserve( self ):

''' Reference: http://sendapatch.se/projects/pylibmc/pooling.html#pylibmc.ClientPool'''

client = self.queue.get(timeout=self.pool_timeout)

try:

yield client

finally:

self.queue.put( client )

print "Queue_size:", self.queue.qsize()

# Intanlise an instance of PoolClient

mc_client_pool = PoolClient( 5, 0, ['127.0.0.1:11211'] )

# Use a memcache client from the pool of memcache client in your apps

with mc_client_pool.reserve() as mc_client:

#do your work here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值