Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #38 from ritksm/develop
Browse files Browse the repository at this point in the history
added close() to servers
  • Loading branch information
lxyu committed Sep 16, 2014
2 parents 285c8a5 + 578fe69 commit bc6e8ef
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions thriftpy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ def __init__(self, processor, trans,
def serve(self):
pass

def close(self):
pass


class TSimpleServer(TServer):
"""Simple single-threaded server that just pumps around one transport."""

def __init__(self, *args):
TServer.__init__(self, *args)
self.closed = False

def serve(self):
self.trans.listen()
Expand All @@ -43,7 +47,7 @@ def serve(self):
iprot = self.iprot_factory.get_protocol(itrans)
oprot = self.oprot_factory.get_protocol(otrans)
try:
while True:
while True and not self.closed:
self.processor.process(iprot, oprot)
except TTransportException:
pass
Expand All @@ -53,17 +57,21 @@ def serve(self):
itrans.close()
otrans.close()

def close(self):
self.closed = True


class TThreadedServer(TServer):
"""Threaded server that spawns a new thread per each connection."""

def __init__(self, *args, **kwargs):
self.daemon = kwargs.pop("daemon", False)
TServer.__init__(self, *args, **kwargs)
self.closed = False

def serve(self):
self.trans.listen()
while True:
while True and not self.closed:
try:
client = self.trans.accept()
t = threading.Thread(target=self.handle, args=(client,))
Expand All @@ -89,3 +97,6 @@ def handle(self, client):

itrans.close()
otrans.close()

def close(self):
self.closed = True

0 comments on commit bc6e8ef

Please sign in to comment.