所以我发布了20个uswgi进程,并且我使用Nginx来加载它们,如detailed here
我注意到,当一个工作人员被击中时,它会在没有看到memcached中需要的结果后加载memcached。此后,该工作人员将在memcached中找到它们。但是其他工作人员仍然不会看到memcached结果,直到他们自己加载它们。
知道会发生什么事吗?这是相关代码:
import memcache
import msgpack
from flask import Flask
app = Flask(__name__, static_url_path = "")
app.config['PROPAGATE_EXCEPTIONS'] = True
class MemCachedWrapper(object):
"""Simple wrapper around memcached to handle translating keys"""
def make_key(self, key):
#is this crazy?? memcached only wants strings and won't accept spaces
return str(key).replace(' ','')
def __init__(self, servers, debug=0, server_max_value_length=1024*1024*3,
flush_on_start=False):
#Set up memcached
#see README.md for instructions on setting server_max_value_length on memcached (-I option)
self.cache = memcache.Client(servers, debug=debug,
server_max_value_length=server_max_value_length)
if flush_on_start:
self.cache.flush_all()
def get(self, key):
return self.cache.get(self.make_key(key))
def get_multi(self, keys):
m = self.make_key
return self.cache.get_multi([m(k) for k in keys])
def set(self, key, value):
return self.cache.set(self.make_key(key), value)
def delete(self, key):
return self.cache.delete(self.make_key(key))
cache =MemCachedWrapper([MEMCACHE_URL], debug=0,
server_max_value_length=MEMCACHE_MAX_VALUE_LENGTH)
def _get_range(key_beg, key_end):
keys = get_keys(key_beg, key_end)
def stream_results():
mc_results = None
for scality_key in keys:
if not mc_results:
mc_results = cache.get_multi([k[0].encode('utf-8') for k in keys])
scality_key = scality_key.encode('utf-8')
obj = mc_results.get(scality_key)
if obj:
yield dvid_key, obj
else:
print "key miss"
response = session.get(SCALITY_URL % scality_key)
cache.set(scality_key, response.content)
yield dvid_key, response.content
return stream_results()
@app.route('/api/keyvalue_range/<key_beg>/<key_end>/', methods=['GET'])
def get_range(key_beg, key_end):
results = _get_range(key_beg, key_end)
packed = msgpack.packb(list(results), use_bin_type=True)
return Response(packed, mimetype='application/x-msgpack')
if __name__ == '__main__':
app.run(host=host, port=PORT, debug=True)
答案 0 :(得分:0)
这不是一个很好的答案,但我最终使用pylibmc作为我的memcached客户端库,它似乎解决了这个问题。不知道发生了什么。
对于上述代码的更改,基本上只是这些行:
import pylibmc as memcache
...
self.cache = memcache.Client(servers, binary=True,
behaviors={"tcp_nodelay": True, "ketama": True, "no_block": True})