Asynchronous mongo driver for Tornado

已查看 15 次
跳至第一个未读帖子

Dan Yamins

未读,
2011年1月12日 17:52:322011/1/12
收件人 mongod...@googlegroups.com
Hi all:

I've written an asynchronous driver for mongodb on Tornado, using non-blocking sockets.    My basic strategy is to take the pymongo implementation cursor implementation, and break apart the send_and_receive_message method into two methods, a send and a receive method.   (See below -- I'm also happy to put the code up on github if people care).     Then the receiving method is attched to a callback of the Tornado IOLoop. 

What I have works.   However, one thing I'm finding is that I having trouble using nonblocking sockets the whole time.   I have to set blocking on the socket before receiving a message, and unblock afterward.    Is this what I should expecting? 

Also, I'd like replace regular sockets with sockets from pyzmq.   Does anyone have any idea if there any obvious stumbling blocks there, esp in terms of message-level structure for MongoDB being compatible with zmq?

thanks
Dan




=-=-=-=-=-=-=Code snippet from my implementation=-=-=-=-=-=-=-=

    def __send_message(self, message):
        """Send a message on the given socket in a nonblocking fashion -- nothing is returned
        """
        sock = self.__socket
        (request_id, data) = message
        X = sock.send(data)
        if X == 0:
            X = sock.send(data)


   def __receive_data_on_socket(self, length, sock):
        """Lowest level receive operation.

        Takes length to receive and repeatedly calls recv until able to
        return a buffer of that length, raising ConnectionFailure on error.
        """
        message = ""
        sock.setblocking(1)
        while len(message) < length:
            chunk = sock.recv(length - len(message))            
            if chunk == "":
                raise ConnectionFailure("connection closed")
            message += chunk
        sock.setblocking(0)
        return message

回复全部
回复作者
转发
0 个新帖子