Getting exception when trying to monkey patch pymongo.connection._Pool

Posted by Creotiv on Stack Overflow See other posts from Stack Overflow or by Creotiv
Published on 2011-01-05T11:49:58Z Indexed on 2011/01/05 11:53 UTC
Read the original article Hit count: 314

Filed under:
|
|

I use pymongo 1.9 on Ubuntu 10.10 with python 2.6.6

When i trying to monkey patch pymongo.connection._Pool i'm getting error on connection: AutoReconnect: could not find master/primary

But when i change _Pool class in pymongo.connection module, it work pretty fine. Even if i copy _Pool implementation from pymongo.connection module and will try to monkey patch by the same code, it still giving same exception.

I need to remove threading.local from _Pool class, because i use gevent and i need to implement Pool for all mongo connections(for all threads).

I use this code:

import pymongo

class GPool:
    """A simple connection pool.

    Uses thread-local socket per thread. By calling return_socket() a
    thread can return a socket to the pool. Right now the pool size is
    capped at 10 sockets - we can expose this as a parameter later, if
    needed.
    """

    # Non thread-locals
    __slots__ = ["sockets", "socket_factory", "pool_size","sock"]
    #sock = None

    def __init__(self, socket_factory):
        self.pool_size = 10
        if not hasattr(self,"sock"):
            self.sock = None
        self.socket_factory = socket_factory
        if not hasattr(self, "sockets"):
            self.sockets = []

    def socket(self):
        # we store the pid here to avoid issues with fork /
        # multiprocessing - see
        # test.test_connection:TestConnection.test_fork for an example
        # of what could go wrong otherwise
        pid = os.getpid()

        if self.sock is not None and self.sock[0] == pid:
            return self.sock[1]

        try:
            self.sock = (pid, self.sockets.pop())
        except IndexError:
            self.sock = (pid, self.socket_factory())

        return self.sock[1]

    def return_socket(self):
        if self.sock is not None and self.sock[0] == os.getpid():
            # There's a race condition here, but we deliberately
            # ignore it.  It means that if the pool_size is 10 we
            # might actually keep slightly more than that.
            if len(self.sockets) < self.pool_size:
                self.sockets.append(self.sock[1])
            else:
                self.sock[1].close()
        self.sock = None


pymongo.connection._Pool = GPool

© Stack Overflow or respective owner

Related posts about python

Related posts about mongodb