python memcached在第一次获取时很慢?

2024-05-13 17:02:37 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的测试:

from time import time
import pylibmc

MEMCACHED_SERVERS = ['127.0.0.1:11211']

# We have no SASL support for memcached, so we can not use authentication
MEMCACHED_USERNAME = None
MEMCACHED_PASSWORD = None

MEMCACHED_POOL_SIZE = 100   # Used by ClientPool: in order to avoid blocking threads, at least the same size as the number of threads using this facility

mc = pylibmc.Client(MEMCACHED_SERVERS, binary=True,
                    username=MEMCACHED_USERNAME, password=MEMCACHED_PASSWORD,
                    behaviors={"tcp_nodelay" : True,
                                "ketama"     : True })

mc_pool = pylibmc.ClientPool(mc, MEMCACHED_POOL_SIZE)
MEMCACHED_BLOCK = True

def test():
    key = 'api-tree'
    s = time()
    for _ in xrange(20):
        with mc_pool.reserve(block=MEMCACHED_BLOCK) as mc:
            mydata = mc.get(key)
        c = time()
        print 'Ellapsed %d' % (c*1000 - s*1000)
        s = c

test()

这是输出:

Ellapsed 388
Ellapsed 3
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2

这种情况经常发生。改变MEMCACHED_POOL_SIZEMEMCACHED_BLOCK都没有任何效果。你知道吗

为什么memcached在第一次获取时速度很慢?我能加快速度吗?你知道吗


Tags: importnonetrueforsizetimeusernamememcached