pylibmc:'断言" ptr-> query_id == query_id +1'函数“"memcached_get_by_key""失败 [英] pylibmc: 'Assertion "ptr->query_id == query_id +1" failed for function "memcached_get_by_key"'
问题描述
我有一个python网络应用程序,该应用程序使用 pylibmc模块连接到内存缓存服务器.如果我每秒对请求进行一次或更慢的测试,则一切正常.但是,如果我每秒发送多个请求,则我的应用程序崩溃,并且我在日志中看到以下内容:
I have a python web app that uses the pylibmc module to connect to a memcached server. If I test my app with requests once per second or slower, everything works fine. If I send more than one request per second, however, my app crashes and I see the following in my logs:
函数"memcached_get_by_key"的断言"ptr-> query_id == query_id +1"失败,可能是程序员错误,query_id未增加.",位于libmemcached/get.cc:107
Assertion "ptr->query_id == query_id +1" failed for function "memcached_get_by_key" likely for "Programmer error, the query_id was not incremented.", at libmemcached/get.cc:107
函数"memcached_get_by_key"的断言"ptr-> query_id == query_id +1"失败,可能是程序员错误,query_id未增加.",位于libmemcached/get.cc:89
Assertion "ptr->query_id == query_id +1" failed for function "memcached_get_by_key" likely for "Programmer error, the query_id was not incremented.", at libmemcached/get.cc:89
任何想法出了什么问题或如何解决?
Any idea what's going wrong or how to fix it?
我的代码如下:
self.mc = pylibmc.Client(
servers=[os.environ.get(MEMCACHE_SERVER_VAR)],
username=os.environ.get(MEMCACHE_USER_VAR),
password=os.environ.get(MEMCACHE_PASS_VAR),
binary=True
)
#...
if (self.mc != None):
self.mc.set(key, stored_data)
#...
page = self.mc.get(key)
推荐答案
这是线程问题. pylibmc客户端不是线程安全的.您应该将代码转换为使用 ThreadMappedPool对象以确保您为每个线程保持单独的连接.像这样:
This is a threading issue. pylibmc clients are not thread-safe. You should convert your code to use a ThreadMappedPool object to ensure you keep a separate connection for each thread. Something like this:
mc = pylibmc.Client(
servers=[os.environ.get(MEMCACHE_SERVER_VAR)],
username=os.environ.get(MEMCACHE_USER_VAR),
password=os.environ.get(MEMCACHE_PASS_VAR),
binary=True
)
self.pool = pylibmc.ThreadMappedPool(mc)
#...
if (self.pool != None):
with self.pool.reserve() as mc:
mc.set(key, stored_data)
#...
if (self.pool != None):
with self.pool.reserve() as mc:
page = mc.get(key)
请确保在线程完成后(可能在析构函数中)调用self.pool.relinquish()
!
Make sure to call self.pool.relinquish()
when the thread is finished, possibly in the destructor!
(在我的情况下,发生这种情况是因为我使用 cherrypy 作为我的网络服务器,而cherrypy分别产生了10个默认情况下可以处理请求的线程.)
(In my case this happened because I was using cherrypy as my web server, and cherrypy spawns 10 separate threads to serve requests by default.)
这篇关于pylibmc:'断言" ptr-> query_id == query_id +1'函数“"memcached_get_by_key""失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!