Package threadutils :: Module ServerInThread'
[hide private]
[frames] | no frames]

Source Code for Module threadutils.ServerInThread'

 1  #coding: iso-8859-1; 
 2  ############################################################################ 
 3  #                                                                          # 
 4  #    This file 'threadutils/ServerInThread.py'                             # 
 5  #    is part of 'SPyRO: Simple Python Remote Objects'                      # 
 6  #    Copyright (C) 2004-2005 by Eric Sadit Téllez Avila                    # 
 7  #    sadit@lsc.fie.umich.mx or donsadit@gmail.com                          # 
 8  #                                                                          # 
 9  #    This program is free software; you can redistribute it and#or modify  # 
10  #    it under the terms of the GNU General Public License as published by  # 
11  #    the Free Software Foundation; either version 2 of the License, or     # 
12  #    (at your option) any later version.                                   # 
13  #                                                                          # 
14  #    This program is distributed in the hope that it will be useful,       # 
15  #    but WITHOUT ANY WARRANTY; without even the implied warranty of        # 
16  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         # 
17  #    GNU General Public License for more details.                          # 
18  #                                                                          # 
19  #    You should have received a copy of the GNU General Public License     # 
20  #    along with this program; if not, write to the                         # 
21  #    Free Software Foundation, Inc.,                                       # 
22  #    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             # 
23  ############################################################################ 
24   
25  from threading import Thread, Lock, Condition 
26  import thread, threading 
27  import warn 
28   
29  try: 
30      True 
31  except: 
32      __builtins__['True'] = 1 
33      __builtins__['False'] = 0 
34   
35 -class ServerInThread(Thread):
36 - def __init__(self, ServerClass, *args, **kwargs):
37 Thread.__init__(self) 38 self.server = ServerClass(*args, **kwargs) 39 self.running = True 40 self.__closed__ = False 41 self.start()
42
43 - def close(self):
44 self.running = False # we need one final request to stop the server :( 45 self.server.close()
46
47 - def run(self):
48 self.server.serve_forever() 49 while self.running: 50 self.server.handle_request()
51
52 - def __del__(self):
53 if not self.__closed__: 54 self.close() 55 self.__closed__ = True
56