def feed_objects(self, objects, getattr=getattr): """ Feeds a sequence of objects which is converted to CSV. For each object the set column names are interpreted as object attributes and used as basis for the CSV data. None values are converted to empty strings, all other attributes are added stringified. """ columns = self.columns if not columns: raise Error, 'no output columns set' rowlen = len(columns) # Create an emtpy table rows = len(objects) rowindices = Tools.trange(rows) t = [None] * rows for i in rowindices: t[i] = [None] * rowlen # Fill the table icols = Tools.irange(columns) for i in rowindices: obj = objects[i] for j, name in icols: t[i][j] = str(getattr(obj, name)) # Quote and join lines t = [self.separator.join(self._quote(x)) for x in t] # Add final CRLF and store CSV text t.append('') self.text = self.text + self.lineend.join(t)
class _modinit: import math l = Tools.frange(0, 1, MAXLOCALITY) for i, factor in Tools.irange(l): _weights[i] = int((math.exp(factor) - 1.0) * 8192) if _debug: print i, '. weight =', _weights[i]
def clear_cache(self): """ Clears the caches used (flushing any data not yet written). """ if self.caching: #self.flush() Tools.method_mapply(self.caches, 'clear', ())
def cut(self, NOM=NOM, DENOM=DENOM): """ Force a cut of the cache's contents. This will make room for at least one new entry. """ if _debug: print ' Cutting down cache size...' cachesize = self.cachesize # Cut the cache down to the entries in recent get history newdata = {} known_key = newdata.has_key data = self.data for id in self.get_history[-self.locality:]: if known_key(id): continue try: newdata[id] = data[id] except KeyError: pass cachesize = len(newdata) if _debug: print ' Size after cut to recent history:', cachesize # Check if cachesize * NOM >= self.max_cachesize * DENOM: # Calculate weights d = {} weights = _weights d_get = d.get for i, id in Tools.irange(self.get_history[-self.locality:]): if not known_key(id): continue d[id] = d_get(id, 0) + weights[i] # Delete all entries left from median ranking = Tools.sortedby(d.items(), 1) if _debug: print ' Ranking:', ranking for id, weight in ranking[:len(d) / 2]: if _debug: print ' Deleting', id, 'with weight =', weight del newdata[id] # Check cachesize = len(newdata) if cachesize * NOM >= self.max_cachesize * DENOM: # Ok, so the smart way didn't work... if _debug: print ' Did not work, going the hard way...' newdata.clear() cachesize = 0 self.data = newdata self.cachesize = cachesize self.cuts = self.cuts + 1
def clear_cache(self): """ Clears the caches used (flushing any data not yet written). """ if self.caching: #self.flush() Tools.method_mapply(self.caches,'clear',())
def rpcdecode(url, prefix='', decode=1, splitat=TextTools.splitat, charsplit=TextTools.charsplit, len=len, tuple=tuple, urldecode=urldecode): """ Decode a RPC encoded function/method call. Returns a tuple (name,args,kws) where args is a tuple of string arguments and kws is a dictionary containing the given keyword parameters or None. All parameters are returned as strings; it is up to the caller to decode them into e.g. integers, etc. If prefix is given and found it is removed from the name prior to returning it. decode can be set to false to prevent the url from being urldecoded prior to processing. The decode function also supports the syntax 'method' instead of 'method()' for calls without arguments. """ if decode: url = urldecode(url) # Decode the method: method[(arg0,arg1,...,kw0=val0,kw1=val1,...)] name, rawargs = splitat(url, '(') if rawargs: # Cut out the pure argument part, ignoring any character after # the final ')' rawargs, rest = splitat(rawargs, ')', -1) # Argument list: split at ',' args = charsplit(rawargs, ',') if '=' in rawargs: kws = {} for i, arg in Tools.reverse(Tools.irange(args)): if '=' in arg: k, v = splitat(arg, '=') kws[k] = v del args[i] else: kws = None args = tuple(args) else: args = () kws = None if prefix: if name[:len(prefix)] == prefix: name = name[len(prefix):] return name, args, kws else: return name, args, kws
def rpcdecode(url,prefix='',decode=1, splitat=TextTools.splitat,charsplit=TextTools.charsplit, len=len,tuple=tuple,urldecode=urldecode): """ Decode a RPC encoded function/method call. Returns a tuple (name,args,kws) where args is a tuple of string arguments and kws is a dictionary containing the given keyword parameters or None. All parameters are returned as strings; it is up to the caller to decode them into e.g. integers, etc. If prefix is given and found it is removed from the name prior to returning it. decode can be set to false to prevent the url from being urldecoded prior to processing. The decode function also supports the syntax 'method' instead of 'method()' for calls without arguments. """ if decode: url = urldecode(url) # Decode the method: method[(arg0,arg1,...,kw0=val0,kw1=val1,...)] name,rawargs = splitat(url,'(') if rawargs: # Cut out the pure argument part, ignoring any character after # the final ')' rawargs,rest = splitat(rawargs,')',-1) # Argument list: split at ',' args = charsplit(rawargs,',') if '=' in rawargs: kws = {} for i,arg in Tools.reverse(Tools.irange(args)): if '=' in arg: k,v = splitat(arg,'=') kws[k] = v del args[i] else: kws = None args = tuple(args) else: args = () kws = None if prefix: if name[:len(prefix)] == prefix: name = name[len(prefix):] return name,args,kws else: return name,args,kws
def free(self, position, OLD=OLD, HOT=HOT): """ Deletes an already written record by marking it OLD. The next garbage collection will make the change permanent and free the occupied space. """ if self.state != HOT: self.mark(HOT) file = self.file file.seek(position + 5) file.write(OLD) if self.caching: Tools.method_mapply(self.caches, 'delete', (position, ))
def feed_list(self, table): """ Feeds a table (list of rows) which is converted to CSV. No more than len(columns) items are written for each row. All rows are filled up with "" entries to have an equal number of items. None entries are converted to empty strings, all other objects are stringified. """ columns = self.columns if columns: rowlen = len(columns) else: # Calculate the max. number of columns in the table rowlen = max(map(len, table)) # Prepare an empty table t = [None] * len(table) _quote = self._quote # Fill in data for i, row in Tools.irange(table): row = _quote(row[:rowlen]) if len(row) < rowlen: row[len(row):] = ['""'] * (rowlen - len(row)) t[i] = self.separator.join(row) # Add final CRLF and add as CSV text t.append('') self.text = self.text + self.lineend.join(t)
def print_stack(file=_sys.stdout,levels=100,offset=0,locals=0): # Prepare frames try: raise ValueError except ValueError: # Go back offset+1 frames... f = _sys.exc_info()[2].tb_frame for i in range(offset + 1): if f.f_back is not None: f = f.f_back # Extract frames frames = [] while f: frames.append(f) f = f.f_back frames.reverse() # Prepare stack stack = _traceback.extract_stack() # Make output file.write('Stack:\n') for (frame,(filename, lineno, name, line)) in \ Tools.tuples(frames,stack)[-levels:]: file.write(' File "%s", line %d, in %s\n' % (filename,lineno,name)) if line: file.write(' %s\n' % line.strip()) if locals: print_frame_locals(frame,file,indent=' |',title='')
def print_recursive(obj,file=_sys.stdout,indent='',levels=1, nonrecursive=(),filter=None): # Filter out nonrecursive types and objects try: if type(obj) in nonrecursive or \ obj in nonrecursive: return except: # Error during compares result in the object not being # printed return # Print the object depending on its interface if hasattr(obj,'__dict__') and \ obj.__dict__ is not None: print_dict(obj.__dict__,file,indent,levels, nonrecursive=nonrecursive, filter=filter) elif hasattr(obj,'items'): print_dict(obj,file,indent,levels,1, nonrecursive=nonrecursive, filter=filter) elif Tools.issequence(obj) and not is_string(obj): print_sequence(obj,file,indent,levels, nonrecursive=nonrecursive) elif hasattr(obj,'__members__'): d = {} for attr in obj.__members__: d[attr] = getattr(obj,attr) print_dict(d, file, indent, levels, nonrecursive=nonrecursive, filter=filter)
def search_bench(word, text): iterations = Tools.trange(COUNT) print ('Searching for all occurences of %r using ...' % word) t0 = time.time() so = TextTools.TextSearch(word) for i in iterations: l = so.findall(text) t1 = time.time() count = len(l) print (' - mx.TextSearch.TextSearch().findall(): %5.3f ms (%i)' % ((t1 - t0) / COUNT * 1000.0, count)) t0 = time.time() so = re.compile(word) for i in iterations: l = so.findall(text) t1 = time.time() count = len(l) print (' - re.compile().findall(): %5.3f ms (%i)' % ((t1 - t0) / COUNT * 1000.0, count)) t0 = time.time() for i in iterations: count = text.count(word) t1 = time.time() print (' - text.count(): %5.3f ms (%i)' % ((t1 - t0) / COUNT * 1000.0, count))
def print_stack(file=_sys.stdout, levels=100, offset=0, locals=0): # Prepare frames try: raise ValueError except ValueError: # Go back offset+1 frames... f = _sys.exc_info()[2].tb_frame for i in range(offset + 1): if f.f_back is not None: f = f.f_back # Extract frames frames = [] while f: frames.append(f) f = f.f_back frames.reverse() # Prepare stack stack = _traceback.extract_stack() # Make output file.write('Stack:\n') for (frame,(filename, lineno, name, line)) in \ Tools.tuples(frames,stack)[-levels:]: file.write(' File "%s", line %d, in %s\n' % (filename, lineno, name)) if line: file.write(' %s\n' % line.strip()) if locals: print_frame_locals(frame, file, indent=' |', title='')
def feed_list(self,table): """ Feeds a table (list of rows) which is converted to CSV. No more than len(columns) items are written for each row. All rows are filled up with "" entries to have an equal number of items. None entries are converted to empty strings, all other objects are stringified. """ columns = self.columns if columns: rowlen = len(columns) else: # Calculate the max. number of columns in the table rowlen = max(map(len,table)) # Prepare an empty table t = [None] * len(table) _quote = self._quote # Fill in data for i,row in Tools.irange(table): row = _quote(row[:rowlen]) if len(row) < rowlen: row[len(row):] = ['""'] * (rowlen - len(row)) t[i] = self.separator.join(row) # Add final CRLF and add as CSV text t.append('') self.text = self.text + self.lineend.join(t)
def free(self,position, OLD=OLD,HOT=HOT): """ Deletes an already written record by marking it OLD. The next garbage collection will make the change permanent and free the occupied space. """ if self.state != HOT: self.mark(HOT) file = self.file file.seek(position + 5) file.write(OLD) if self.caching: Tools.method_mapply(self.caches,'delete',(position,))
def objects(self, constructor): """ Builds a list of objects by calling the given constructor with keywords defined by mapping column names to values for each input line. .columns must have been set using .set_columns() or by processing a given CSV header. """ lines = self.lines keys = self.columns if keys is None: raise Error, 'no columns set' objs = [None] * len(lines) for i, line in Tools.irange(lines): kws = dict(Tools.tuples(keys, line)) objs[i] = apply(constructor, (), kws) return objs
def objects(self,constructor): """ Builds a list of objects by calling the given constructor with keywords defined by mapping column names to values for each input line. .columns must have been set using .set_columns() or by processing a given CSV header. """ lines = self.lines keys = self.columns if keys is None: raise Error,'no columns set' objs = [None] * len(lines) for i,line in Tools.irange(lines): kws = dict(Tools.tuples(keys, line)) objs[i] = apply(constructor,(),kws) return objs
def feed_dict(self,table,rows=None): """ Feeds a table (dict of lists) which is converted to CSV. Only the keys set as column names are used to form the CSV data. All lists in the dictionary must have equal length or at least rows number of entries, if rows is given. None entries are converted to empty strings, all other objects are stringified. """ columns = self.columns if not columns: raise Error,'no output columns set' rowlen = len(columns) # Create an emtpy table if not rows: rows = 0 for column in columns: nrows = len(table[column]) if nrows > rows: rows = nrows rowindices = Tools.trange(rows) t = [None] * rows for i in rowindices: t[i] = [None] * rowlen # Fill the table for j,k in Tools.irange(columns): for i in rowindices: t[i][j] = table[k][i] # Quote and join lines t = [self.separator.join(self._quote(x)) for x in t] # Add final CRLF and store CSV text t.append('') self.text = self.text + self.lineend.join(t)
def _quote(self, line, str=str): """ CSV style quote the given line of text. """ nline = ['""'] * len(line) for i, item in Tools.irange(line): if item is not None: text = str(item) else: text = '' nline[i] = '"%s"' % text.replace('"', '""') return nline
def _unquote(self, line): """ Unquote a CSV style quoted line of text. Internal method. Do not use directly. """ for i, text in Tools.irange(line): if text[:1] == '"' and text[-1:] == '"': text = text[1:-1] line[i] = text.replace('""', '"') return line
def _unquote(self,line): """ Unquote a CSV style quoted line of text. Internal method. Do not use directly. """ for i,text in Tools.irange(line): if text[:1] == '"' and text[-1:] == '"': text = text[1:-1] line[i] = text.replace('""','"') return line
def dictionary(self): """ Return the current data as dictionary of lists of strings, with one entry for each column. .columns must have been set using .set_columns() or by processing a given CSV header. """ table = {} lines = self.lines keys = self.columns if keys is None: raise Error, 'no columns set' rows = len(lines) for k in keys: table[k] = [None] * rows for i, key in Tools.irange(keys): column = table[key] for j, row in Tools.irange(lines): if len(row) > i: column[j] = row[i] return table
def feed_objects(self,objects, getattr=getattr): """ Feeds a sequence of objects which is converted to CSV. For each object the set column names are interpreted as object attributes and used as basis for the CSV data. None values are converted to empty strings, all other attributes are added stringified. """ columns = self.columns if not columns: raise Error,'no output columns set' rowlen = len(columns) # Create an emtpy table rows = len(objects) rowindices = Tools.trange(rows) t = [None] * rows for i in rowindices: t[i] = [None] * rowlen # Fill the table icols = Tools.irange(columns) for i in rowindices: obj = objects[i] for j,name in icols: t[i][j] = str(getattr(obj, name)) # Quote and join lines t = [self.separator.join(self._quote(x)) for x in t] # Add final CRLF and store CSV text t.append('') self.text = self.text + self.lineend.join(t)
def dictionary(self): """ Return the current data as dictionary of lists of strings, with one entry for each column. .columns must have been set using .set_columns() or by processing a given CSV header. """ table = {} lines = self.lines keys = self.columns if keys is None: raise Error,'no columns set' rows = len(lines) for k in keys: table[k] = [None] * rows for i, key in Tools.irange(keys): column = table[key] for j, row in Tools.irange(lines): if len(row) > i: column[j] = row[i] return table
def filter_header(self, header, lower=TextTools.lower): """ Filter the given header line. The base class converts the column names to all lowercase and removes any whitespace included in the header. This method is only called in case the header was read from the data provided to the object. """ l = [''] * len(header) for i, column in Tools.irange(header): l[i] = ''.join(lower(column).split()) return l
def _quote(self, line, str=str): """ CSV style quote the given line of text. """ nline = ['""'] * len(line) for i,item in Tools.irange(line): if item is not None: text = str(item) else: text = '' nline[i] = '"%s"' % text.replace('"','""') return nline
def print_sequence(obj, file=_sys.stdout, indent='', levels=2, nonrecursive=()): l = [] unfold = 0 try: length = len(obj) except (AttributeError, ValueError, TypeError): return for i in Tools.trange(min(length, _VALUE_LEN_LIMIT)): try: value = obj[i] except: break try: r = repr(value) except: r = '*repr()-error*' # Truncate if len(r) > _VALUE_LEN_LIMIT: r = r[:_VALUE_LEN_LIMIT] + '...' # Write value l.append((value, r)) # Only unfold sequences that have non-string items or string items # with more than on character if not is_string(value) or len(value) > 1: unfold = 1 if len(obj) > _VALUE_LEN_LIMIT: l.append(('...', '...truncated...')) # Unfold value object if unfold: for i, (value, rvalue) in irange(l): file.write('%s%-15s = %s\n' % (indent, '[%i]' % i, rvalue)) if levels > 1: print_recursive(value, file, indent + ' ', levels - 1, nonrecursive=nonrecursive)
def list(self): """ Return the current data as list of lists, each having self.width string entries. Missing entries are set to None. """ width = self.width lines = self.lines table = [None] * len(lines) for i, row in Tools.irange(lines): row = row[:] if len(row) < width: row[len(row):] = [None] * (width - len(row)) table[i] = row return table
def _quote(self, line, str=str): """ CSV style quote the given line of text. """ nline = ['""'] * len(line) for i,item in Tools.irange(line): if item is None: text = '' elif isinstance(item, unicode): text = item.encode(self.encoding) else: text = str(item) nline[i] = '"%s"' % text.replace('"','""') return nline
def list(self): """ Return the current data as list of lists, each having self.width string entries. Missing entries are set to None. """ width = self.width lines = self.lines table = [None] * len(lines) for i, row in Tools.irange(lines): row = row[:] if len(row) < width: row[len(row):] = [None]*(width-len(row)) table[i] = row return table
def filter_header(self, header, lower=TextTools.lower): """ Filter the given header line. The base class converts the column names to all lowercase and removes any whitespace included in the header. This method is only called in case the header was read from the data provided to the object. """ l = [''] * len(header) for i,column in Tools.irange(header): l[i] = ''.join(lower(column).split()) return l
def __str__(self): lines = self.list() desc = self.description() width = 0 output = [] write = output.append for col in desc: write('%-*s|' % (col[1], col[0])) write('\n') for col in desc: write('=' * col[1] + '+') write('\n') for line in lines: for i, item in Tools.irange(line): write('%-*s|' % (desc[i][1], item)) write('\n') return ''.join(output)
def __str__(self): lines = self.list() desc = self.description() width = 0 output = [] write = output.append for col in desc: write('%-*s|' % (col[1],col[0])) write('\n') for col in desc: write('=' * col[1] + '+') write('\n') for line in lines: for i,item in Tools.irange(line): write('%-*s|' % (desc[i][1],item)) write('\n') return ''.join(output)
def print_sequence(obj,file=_sys.stdout,indent='',levels=2, nonrecursive=()): l = [] unfold = 0 try: length = len(obj) except (AttributeError, ValueError, TypeError): return for i in Tools.trange(min(length,_VALUE_LEN_LIMIT)): try: value = obj[i] except: break try: r = repr(value) except: r = '*repr()-error*' # Truncate if len(r) > _VALUE_LEN_LIMIT: r = r[:_VALUE_LEN_LIMIT] + '...' # Write value l.append((value,r)) # Only unfold sequences that have non-string items or string items # with more than on character if not is_string(value) or len(value) > 1: unfold = 1 if len(obj) > _VALUE_LEN_LIMIT: l.append(('...','...truncated...')) # Unfold value object if unfold: for i,(value,rvalue) in irange(l): file.write('%s%-15s = %s\n' % (indent, '[%i]' % i, rvalue)) if levels > 1: print_recursive(value,file,indent + ' ',levels-1, nonrecursive=nonrecursive)
def description(self, header=1): """ Return a list of tuples (column name, max length) found in the data. If header is true (default), the column names themselves are included in the calculation. """ lines = self.lines columns = self.columns width = len(columns) if header: lengths = [] for column in columns: lengths.append(len(column)) else: lengths = [0] * width for row in self.lines: for i, o in Tools.irange(row[:width]): if len(o) > lengths[i]: lengths[i] = len(o) return map(None, columns, lengths)
def description(self, header=1): """ Return a list of tuples (column name, max length) found in the data. If header is true (default), the column names themselves are included in the calculation. """ lines = self.lines columns = self.columns width = len(columns) if header: lengths = [] for column in columns: lengths.append(len(column)) else: lengths = [0] * width for row in self.lines: for i,o in Tools.irange(row[:width]): if len(o) > lengths[i]: lengths[i] = len(o) return map(None,columns,lengths)
class WorkerProcess(object): """ Worker process encapsulation. These work a lot like server processes, except that they are managed by daemon process as child processes. The implementation uses two contexts: - the server daemon context in which .start_server() and .stop_server() are called - the worker process contect in which .main() is run The .main() method has to be overridden to implement the worker process logic. """ # Note: This code is similar to ServerDaemon, but for worker # processes we don't fork twice since we want the workers to be # child processes of the server process. # Name of the worker name = 'Worker Process' # PID of the worker process; set in both the server and the worker # process context pid = 0 # Started flag. Set by .start_worker()/.stop_worker() in the # server context. started = False # Exit status code. Set by .worker_exited() in the server context. exit_status = 0 # mxLog object to use. Inherited from the ServerDaemon if None log = None # Log id to use in the worker process. Inherited from the # ServerDaemon if None log_id = None # Process name to use for the worker process. Note: this is not # guaranteed to work. Inherited from the ServerDaemon if None process_name = None # Startup time of the worker processes in seconds. The # .start_worker() method will wait this number of second for the # worker process to start up. worker_startup_time = 2 # Shutdown time of the worker processes in seconds. The # .stop_worker() method will wait this number of second for the # worker processes to terminate. worker_shutdown_time = 2 # Kill time of the worker processes in seconds. The .stop_worker() # method will wait this number of second for the worker processes # to terminate after having received the KILL signal. worker_kill_time = 1 # Range of file descriptors to close after the fork; all open fds # except of stdin, stdout, stderr close_file_descriptors = tuple(range(3, 99)) def __init__(self, server_daemon): # Inherit settings from the server if self.log is None: self.log = server_daemon.log if self.log is None: self.log = Log.LogNothing if self.log_id is None: self.log_id = server_daemon.log_id if self.process_name is None: self.process_name = server_daemon.process_name def __repr__(self): return '%s(%s with PID %s)' % (self.__class__.__name__, self.name, self.pid) def setup_worker(self, **parameters): """ Prepare the worker startup and adjust the parameters to be passed on to the worker's .main() method. This method is called by .start_worker() before forking off a child process in order to give the WorkerProcess implementation a chance to adjust itself to the parameters. It has to return a copy of the parameters keyword argument dictionary. This method is called in the context of the server. """ return parameters.copy() def start_worker(self, **parameters): """ Start the worker process and pass the given keyword parameters to the .main() method. """ # Prepare startup parameters = self.setup_worker(**parameters) assert parameters is not None, \ '.setup_worker() did not return a parameters dictionary' # Flush file descriptors sys.stderr.flush() sys.stdout.flush() # Create a socket pair server_socket, worker_socket = socket.socketpair( socket.AF_UNIX, socket.SOCK_STREAM) # Fork a child process, errors will be reported to the caller pid = os.fork() if pid != 0: ### Server process context ... # Close our end of the socket pair server_socket.close() # Wait for the child to start up worker_socket.settimeout(self.worker_startup_time) try: ok = worker_socket.recv(1) except socket.timeout: ok = None worker_socket.close() if not ok: # Terminate the child, if it didn't startup in time self.log( self.log.ERROR, '%s: ' 'Collecting worker process PID %s due to startup failure', self.name, pid) try: self._kill_worker(pid) except WorkerNotStoppedError: pass # Report the failure raise WorkerNotStartedError( '%s: Worker process with PID %s did not start up' % (self.name, pid)) # Remember the worker process pid and return it self.pid = pid self.started = True self.exit_status = 0 return pid ### Worker process context ... # Close our end of the socket pair worker_socket.close() # Close all open fds except of stdin, stdout, stderr self.log.close() server_socket_fd = server_socket.fileno() for i in self.close_file_descriptors: if i == server_socket_fd: # We'll close that manually later on continue try: os.close(i) except (IOError, OSError), reason: pass # Reopen the log file self.log.open() if self.log_id: self.log.setup(log_id=self.log_id) # Redirect stdout and stderr to the log file self.log.redirect_stdout() self.log.redirect_stderr() # Try to rename the process if self.process_name: try: Tools.setproctitle(self.process_name) except AttributeError: pass # Set the PID of the worker process self.pid = os.getpid() # Let the server process know that we've started up server_socket.send('1') server_socket.close() # Run the .main() method rc = 0 try: try: self.log(self.log.INFO, '%s: Worker process PID %s %s', self.name, self.pid, '-' * 40) if _debug > 1: self.log.object( self.log.DEBUG, '%s: Using the following startup parameters:' % self.name, parameters) # Run the worker's .main() method main_rc = self.main(**parameters) # Return the exit code, if it's an integer if main_rc is not None and isinstance(main_rc, int): rc = main_rc except Exception: # Something unexpected happened... log the problem and exit self.log.traceback(self.log.ERROR, '%s: ' 'Unexpected worker process error:', self.name) rc = 1 finally: self.cleanup_worker() # Exit process os._exit(rc)
class ServerDaemon(object): """ Server daemon encapsulation. This class provides an easy way to setup a Unix server daemon that uses a single process. It may still spawn off additional processes, but this encapsulation only manages the main process. The implementation runs two contexts: - the control context in which .start_server() and .stop_server() are called - the server process contect in which .main() is run """ # Name of the server name = 'Server Daemon' # PID of the process pid = 0 # Location of the PID file of the parent process pid_file = 'server.pid' # umask to set for the forked server process umask = 022 # Root dir to change to for the forked server process root_dir = '' # Range of file descriptors to close after the fork; all open fds # except of stdin, stdout, stderr close_file_descriptors = tuple(range(3, 99)) # mxLog object to use log = Log.log # Log id to use in the forked server process log_id = '' # Process name to use for the forked server process. Note: this is # not guaranteed to work process_name = '' # Server startup time in seconds. The .start_server() # method will wait at most this number of seconds for the main # server process to initialize and enter the .main() method. This # includes forking overhead, module import times, etc. It does not # cover the startup time that the server may need to become usable # for external applications. The startup time can be configured # with .server_startup_time server_startup_time = 2 # Startup initialization time of the server in seconds. The # .start_server() method will unconditionally wait this number of # seconds after having initialized the server in order to give the # .main() method a chance to setup any resources it may need to # initialize. server_startup_init_time = 0 # Server shutdown time in seconds. The .stop_server() # method will wait at most this number of seconds for the main # server process to terminate after sending it a TERM signal. server_shutdown_time = 2 # Kill time of the server processes in seconds. The .stop_server() # method will wait this number of second for the worker processes # to terminate after having received the KILL signal. server_kill_time = 1 # Shutdown cleanup time of the server in seconds. The # .stop_server() method will unconditionally wait this number of # seconds after having terminated the main server process in order # to give possibly additionally spawned processes a chance to # terminate cleanly as well. server_shutdown_cleanup_time = 0 ### def setup_server(self, **parameters): """ Prepare the server startup and adjust the parameters to be passed on to the server's .main() method. This method is called by .start_server() before forking off a child process in order to give the WorkerProcess implementation a chance to adjust itself to the parameters. It has to return a copy of the parameters keyword argument dictionary. This method is called in the context of the server. """ return parameters.copy() def _kill_server(self, pid): """ Kill a server process pid and collect it. Returns the process exit status or -1 in case this cannot be determined. Raises a ServerNotStoppedError in case the process cannot be stopped. """ try: return kill_process(pid, shutdown_time=self.server_shutdown_time, kill_time=self.server_kill_time, log=self.log, log_prefix='%s: ' % self.name) except ProcessNotStoppedError: # Did not work out... raise ServerNotStoppedError( '%s: Server process with PID %s did not stop' % (self.name, pid)) def start_server(self, **parameters): """ Starts the server. Keyword parameters are passed on to the forked process' .main() method. Returns the PID of the started server daemon. Raises a ServerAlreadyRunningError if the server is already running. Raises a ServerNotStartedError in case the daemon could not be started. """ # Verify if we have a running server process pid = self.server_status() if pid is not None: raise ServerAlreadyRunningError( 'Server is already running (PID %s)' % pid) # Prepare startup parameters = self.setup_server(**parameters) assert parameters is not None, \ '.setup_server() did not return a parameters dictionary' # Flush the standard file descriptors sys.stderr.flush() sys.stdout.flush() # Fork a child process, errors will be reported to the caller pid = os.fork() if pid != 0: ### Parent process # Collect the first child if _debug: self.log( self.log.DEBUG, '%s: ' 'Waiting for the first child with PID %s to terminate', self.name, pid) os.waitpid(pid, 0) # Wait a few seconds until the server has started if _debug: self.log(self.log.DEBUG, '%s: ' 'Waiting for the server process to startup', self.name) for i in xrange(int(self.server_startup_time * 100) + 1): spid = self.server_status() if spid is not None: break time.sleep(0.01) else: # Server did not startup in time: terminate the first # child self.log(self.log.ERROR, '%s: Server process failed to startup', self.name) try: self._kill_server(pid) except ServerNotStoppedError: pass # Report the problem; XXX Note that the second child # may still startup after this first has already # terminated. raise ServerNotStartedError('%s did not start up' % self.name) if self.server_startup_init_time: time.sleep(self.server_startup_init_time) return spid ### This is the first child process # Daemonize process os.setpgrp() if self.root_dir: os.chdir(self.root_dir) if self.umask: os.umask(self.umask) try: # Try to become a session leader os.setsid() except OSError: # We are already the process session leader pass # Close all open fds except of stdin, stdout, stderr self.log.close() for i in self.close_file_descriptors: try: os.close(i) except (IOError, OSError), reason: pass # Fork again to become a separate daemon process pid = os.fork() if pid != 0: # We need to terminate the "middle" process at this point, since we # don't want to continue with two instances of the original caller. # We must not call any cleanup handlers here. os._exit(0) ### This is the second child process: the server daemon # Turn the daemon into a process group leader os.setpgrp() # Reopen the log file self.log.open() if self.log_id: self.log.setup(log_id=self.log_id) # Redirect stdout and stderr to the log file self.log.redirect_stdout() self.log.redirect_stderr() # Try to rename the process if self.process_name: try: Tools.setproctitle(self.process_name) except AttributeError: pass # Save the PID of the server daemon process self.pid = os.getpid() self.save_server_pid(self.pid) # We need to remove the PID file on exit rc = 0 try: try: self.log(self.log.INFO, '%s: Server process PID %s %s', self.name, self.pid, '*' * 60) # Run the server's .main() method main_rc = self.main(**parameters) # Return the exit code, if it's an integer if main_rc is not None and isinstance(main_rc, int): rc = main_rc except SystemExit, exc: # Normal shutdown rc = exc.code self.log(self.log.INFO, '%s: Shutting down with status: %s', self.name, rc) except Exception: # Something unexpected happened... log the problem and exit self.log.traceback(self.log.ERROR, '%s: Unexpected server error:', self.name) rc = 1
def _set_log_id(self, value): self._custom_log_id = value log_id = property(_get_log_id, _set_log_id) ### LogNothing disabled logging completely class LogNothing(Log): # Don't log anything ignore_level = SYSTEM_LOG_NOTHING def setup(self, *args, **kws): Log.setup(*args, **kws) self.ignore_level = SYSTEM_LOG_NOTHING def open(self, flags='a'): self.open_log_file = None def close(self): pass ### # Create a main log object if __debug__ and Tools.debugging(): log = Log(SYSTEM_LOG_EVERYTHING) else: log = Log(SYSTEM_INFO + 1)
def cut(self, NOM=NOM,DENOM=DENOM): """ Force a cut of the cache's contents. This will make room for at least one new entry. """ if _debug: print ' Cutting down cache size...' cachesize = self.cachesize # Cut the cache down to the entries in recent get history newdata = {} known_key = newdata.has_key data = self.data for id in self.get_history[-self.locality:]: if known_key(id): continue try: newdata[id] = data[id] except KeyError: pass cachesize = len(newdata) if _debug: print ' Size after cut to recent history:',cachesize # Check if cachesize * NOM >= self.max_cachesize * DENOM: # Calculate weights d = {} weights = _weights d_get = d.get for i,id in Tools.irange(self.get_history[-self.locality:]): if not known_key(id): continue d[id] = d_get(id,0) + weights[i] # Delete all entries left from median ranking = Tools.sortedby(d.items(),1) if _debug: print ' Ranking:',ranking for id,weight in ranking[:len(d)/2]: if _debug: print ' Deleting',id,'with weight =',weight del newdata[id] # Check cachesize = len(newdata) if cachesize * NOM >= self.max_cachesize * DENOM: # Ok, so the smart way didn't work... if _debug: print ' Did not work, going the hard way...' newdata.clear() cachesize = 0 self.data = newdata self.cachesize = cachesize self.cuts = self.cuts + 1
log_id = property(_get_log_id, _set_log_id) ### LogNothing disabled logging completely class LogNothing(Log): # Don't log anything ignore_level = SYSTEM_LOG_NOTHING def setup(self, *args, **kws): Log.setup(*args, **kws) self.ignore_level = SYSTEM_LOG_NOTHING def open(self, flags='a'): self.open_log_file = None def close(self): pass ### # Create a main log object if __debug__ and Tools.debugging(): log = Log(SYSTEM_LOG_EVERYTHING) else: log = Log(SYSTEM_INFO + 1)