def c_library_server(lock, conn): """The GRASS C-libraries server function designed to be a target for multiprocessing.Process :param lock: A multiprocessing.Lock :param conn: A multiprocessing.Pipe """ # Crerate the function array functions = [0] * 15 functions[RPCDefs.STOP] = _stop functions[RPCDefs.HAS_TIMESTAMP] = _has_timestamp functions[RPCDefs.WRITE_TIMESTAMP] = _write_timestamp functions[RPCDefs.READ_TIMESTAMP] = _read_timestamp functions[RPCDefs.REMOVE_TIMESTAMP] = _remove_timestamp functions[RPCDefs.READ_MAP_INFO] = _read_map_info functions[RPCDefs.MAP_EXISTS] = _map_exists functions[RPCDefs.AVAILABLE_MAPSETS] = _available_mapsets functions[RPCDefs.GET_DRIVER_NAME] = _get_driver_name functions[RPCDefs.GET_DATABASE_NAME] = _get_database_name functions[RPCDefs.G_MAPSET] = _get_mapset functions[RPCDefs.G_LOCATION] = _get_location functions[RPCDefs.G_GISDBASE] = _get_gisdbase functions[RPCDefs.G_FATAL_ERROR] = _fatal_error libgis.G_gisinit("c_library_server") libgis.G_debug(1, "Start C-interface server") while True: # Avoid busy waiting conn.poll(4) data = conn.recv() lock.acquire() functions[data[0]](lock, conn, data) lock.release()
def message_server(lock, conn): """The GRASS message server function designed to be a target for multiprocessing.Process :param lock: A multiprocessing.Lock :param conn: A multiprocessing.Pipe This function will use the G_* message C-functions from grass.lib.gis to provide an interface to the GRASS C-library messaging system. The data that is send through the pipe must provide an identifier string to specify which C-function should be called. The following identifers are supported: - "INFO" Prints an info message, see G_message() for details - "IMPORTANT" Prints an important info message, see G_important_message() for details - "VERBOSE" Prints a verbose message if the verbosity level is set accordingly, see G_verbose_message() for details - "WARNING" Prints a warning message, see G_warning() for details - "ERROR" Prints a message with a leading "ERROR: " string, see G_important_message() for details - "PERCENT" Prints a percent value based on three integer values: n, d and s see G_percent() for details - "STOP" Stops the server function and closes the pipe - "FATAL" Calls G_fatal_error(), this functions is only for testing purpose The that is end through the pipe must be a list of values: - Messages: ["INFO|VERBOSE|WARNING|ERROR|FATAL", "MESSAGE"] - Debug: ["DEBUG", level, "MESSAGE"] - Percent: ["PERCENT", n, d, s] """ libgis.G_debug(1, "Start messenger server") while True: # Avoid busy waiting conn.poll(None) data = conn.recv() message_type = data[0] # Only one process is allowed to write to stderr lock.acquire() # Stop the pipe and the infinite loop if message_type == "STOP": conn.close() lock.release() libgis.G_debug(1, "Stop messenger server") sys.exit() message = data[1] # libgis limitation if isinstance(message, type(" ")): if len(message) >= 2000: message = message[:1999] if message_type == "PERCENT": n = int(data[1]) d = int(data[2]) s = int(data[3]) libgis.G_percent(n, d, s) elif message_type == "DEBUG": level = data[1] message = data[2] libgis.G_debug(level, message) elif message_type == "VERBOSE": libgis.G_verbose_message(message) elif message_type == "INFO": libgis.G_message(message) elif message_type == "IMPORTANT": libgis.G_important_message(message) elif message_type == "WARNING": libgis.G_warning(message) elif message_type == "ERROR": libgis.G_important_message("ERROR: %s" % message) # This is for testing only elif message_type == "FATAL": libgis.G_fatal_error(message) lock.release()
def _stop(lock, conn, data): libgis.G_debug(1, "Stop C-interface server") conn.close() lock.release() sys.exit()