从memcache读取数据有时会失败

发布于 2024-09-27 06:22:58 字数 1288 浏览 4 评论 0原文

我编写了一个基于 gevent 的程序,允许其 Web 客户端通过它快速交换消息(因此它的工作方式就像一个集线器)。

由于我目前仅支持轮询机制,因此我将其编写为将需要传递到特定客户端的消息存储在服务器端的“收件箱”中。虽然客户端列表存储在 MySQL 中,但这些收件箱存储在 memcache 中以便更快地访问。当客户端连接到集线器时,它会提取收件箱中累积的所有消息。

问题
问题是,曾几何时,收件人在提取收件箱内容时收不到消息 - 他们收到一个空数组。
更让我困惑的是,如果我重新启动集线器,客户端未收到的消息将突然出现并被传递到目的地。
如果我的代码中有明显的缺陷,您能指出吗?您对此有何解释?

push 是执行将消息放入客户端收件箱的方法。 pull 是将所有累积消息的列表作为列表检索并将其返回到主处理函数的方法。

def __push(self, domain, message, tid=None):
    if tid:
        try:
            messages = self.mc.get("%s_inbox" % tid.encode('utf8'))
        except:
            logging.error("__push memcached failure", exc_info=1)
        if messages:
            messages = fromjson(messages)
            messages.append(message)
            self.mc.set("%s_inbox" % tid.encode('utf8'), tojson(messages))
            print "Pushed to", "%s_inbox" % tid.encode('utf8')


def __pull(self, tid):
    try:
        messages = self.mc.get("%s_inbox" % tid.encode('utf8'))
        if messages:
            self.mc.set("%s_inbox" % tid.encode('utf8'), "[]")
            return fromjson(messages)
        else:
            return []
    except:
        logging.error("__pull failure", exc_info=1)
        return []

I've written a gevent-based program that allows its web clients to quickly exchange messages through it (so it works like a hub).

Since I only support polling mechanism at this moment, I've written it to store messages that need to be delivered to a specific client in its 'inbox' at the server side. While the client list is stored in MySQL, these inboxes are stored in memcache for faster access. When a client connects to the hub, it pulls all the messages that have accumulated in its inbox.

The question
The problem is that once upon a short while the recipients do not receive their messages when pulling the contents of their inbox - they receive an empty array.
What puzzles me even more is that if I restart the hub, the messages that were not received by the clients will suddenly materialize and get delivered to their destinations.
Can you point me if there's a glaring defect in my code? Do you have any explanation to this effect?

push is the method that gets executed to place a message into a client's inbox. pull is the method that retrieves the list of all the accumulated messages as a list and returns it to the main processing function.

def __push(self, domain, message, tid=None):
    if tid:
        try:
            messages = self.mc.get("%s_inbox" % tid.encode('utf8'))
        except:
            logging.error("__push memcached failure", exc_info=1)
        if messages:
            messages = fromjson(messages)
            messages.append(message)
            self.mc.set("%s_inbox" % tid.encode('utf8'), tojson(messages))
            print "Pushed to", "%s_inbox" % tid.encode('utf8')


def __pull(self, tid):
    try:
        messages = self.mc.get("%s_inbox" % tid.encode('utf8'))
        if messages:
            self.mc.set("%s_inbox" % tid.encode('utf8'), "[]")
            return fromjson(messages)
        else:
            return []
    except:
        logging.error("__pull failure", exc_info=1)
        return []

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2024-10-04 06:22:58

我想我明白了:这是 python-memcache 模块中的一个 bug

I think I got it: it's a bug in the python-memcache module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文