Created
April 9, 2010 04:08
-
-
Save alecthomas/360888 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import gevent | |
from thrift.server.TServer import TServer | |
# XXX Hackish, but should be safe: monkey patch gevent socket support into | |
# Thrift. Overall I think this is cleaner than reimplementing all of TSocket. | |
from thrift.transport import TSocket; TSocket.socket = gevent.socket | |
from thrift.transport.TTransport import TTransportException | |
class TGEventServer(TServer): | |
"""Gevent socket server.""" | |
def serve(self): | |
self.serverTransport.listen() | |
while True: | |
client = self.serverTransport.accept() | |
gevent.spawn(self._process_socket, client) | |
def _process_socket(self, client): | |
"""A greenlet for handling a single client.""" | |
itrans = self.inputTransportFactory.getTransport(client) | |
otrans = self.outputTransportFactory.getTransport(client) | |
iprot = self.inputProtocolFactory.getProtocol(itrans) | |
oprot = self.outputProtocolFactory.getProtocol(otrans) | |
try: | |
while True: | |
self.processor.process(iprot, oprot) | |
except TTransportException, e: | |
pass | |
except Exception, e: | |
logging.exception('Thrift processor failure.') | |
itrans.close() | |
otrans.close() |
Updated to remove flam references.
imlucas also has a forked version with other enhancements.
I am learning how to use gevent in thrift server and come across your code here. my question is that why doing the hacking TSocket.socket = gevent.socket
? some specific reasons?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is the flam.Log object required? If so, can you provide a link to the flam project?