Skip to content

Instantly share code, notes, and snippets.

@alecthomas
Created April 9, 2010 04:08
Show Gist options
  • Save alecthomas/360888 to your computer and use it in GitHub Desktop.
Save alecthomas/360888 to your computer and use it in GitHub Desktop.
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()
@OldhamMade
Copy link

Is the flam.Log object required? If so, can you provide a link to the flam project?

@alecthomas
Copy link
Author

Updated to remove flam references.

imlucas also has a forked version with other enhancements.

@alexandnpu
Copy link

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