Package threadutils
[hide private]
[frames] | no frames]

Source Code for Package threadutils

 1  #coding: iso-8859-1; 
 2  ############################################################################ 
 3  #                                                                          # 
 4  #    This file 'threadutils/__init__.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 PeriodicExecution import * 
26  from ServerInThread import * 
27  from ThreadPool import * 
28  import warn 
29  warn = warn.Warn('threadutils') 
30   
31  __doc__ = """ Thread utilities or useful classes based on thread usage """ 
32  __version__ = '0.1' 
33   
34  __all__ = [ "PeriodicExecution", "ServerInThread", "ThreadPool", "ThreadPoolShared", "forkdaemon"] 
35   
36  import os, sys 
37   
38 -def forkdaemon(new_stdout=None,new_stderr=None,new_stdin=None, 39 new_cwd=os.getcwd(),msg=None):
40 """ A simple (not double fork) daemon generator. """ 41 try: 42 child = os.fork() 43 if child > 0: 44 sys.exit(0) 45 except OSError, e: 46 warn.warn("Fork: %d (%s)" % (e.errno, e.strerror)) 47 sys.exit(1) 48 os.chdir(new_cwd) 49 os.setsid() 50 os.umask(0) 51 if new_stderr is None: 52 errfile = open("/dev/null","wb+") 53 else: 54 errfile = new_stderr 55 56 if new_stdout is None: 57 outfile = open("/dev/null","wb+") 58 else: 59 outfile = new_stdout 60 61 if new_stdin is None: 62 infile = open("/dev/null","rb") 63 else: 64 infile = new_stdin 65 os.dup2(errfile.fileno(), sys.stderr.fileno()) 66 os.dup2(outfile.fileno(), sys.stdout.fileno()) 67 os.dup2( infile.fileno(), sys.stdin.fileno())
68