from gevent.server import StreamServer
import os


def mangodb(socket, address):
    socket.sendall('HELLO\r\n')
    client = socket.makefile()
    output = open('/dev/null', 'w')
    while True:
        line = client.readline()
        if not line:
            break
        cmd_bits = line.split(' ', 1)
        cmd = cmd_bits[0]
        if cmd == 'BYE':
            break
        if len(cmd_bits) > 1:
            output.write(cmd_bits[1])
            if os.environ.get('MANGODB_DURABLE', False):
                output.flush()
                os.fsync(output.fileno())
            client.write('OK' + os.urandom(1024) + '\r\n')
        client.flush()


if __name__ == '__main__':
    server = StreamServer(('0.0.0.0', 27017), mangodb)
    print ('Starting MangoDB on port 27017')
    server.serve_forever()

from gevent.server import StreamServer
import os


def mangodb(socket, address):
    socket.sendall('HELLO\r\n')
    client = socket.makefile()
    output = open('/dev/null', 'w')
    while 1:
        line = client.readline()
        if not line:
            break
        cmd_bits = line.split(' ', 1)
        cmd = cmd_bits[0]
        if cmd == 'BYE':
            break
        if len(cmd_bits) > 1:
            output.write(cmd_bits[1])
            if os.environ.get('MANGODB_DURABLE', False):
                output.flush()
                os.fsync(output.fileno())
            client.write('OK' + os.urandom(1024) + '\r\n')
        client.flush()


if __name__ == '__main__':
    server = StreamServer(('0.0.0.0', 27017), mangodb)
    print ('Starting MangoDB on port 27017')
    server.serve_forever()

Replace `while True` with `while 1` for more web scale sauce.
Replace `while True` with `while 1` for more web scale sauce.

In Python 2.x, True is not a keyword, so it can be reassigned. Compiler replaces `while 1` loop with a single jump, so it is faster by 10%.

http://www.reddit.com/r/Python/comments/ppote/how_would_you_explain_a_performance_gap_between/