def __init__(self): self.__host = "localhost" self.__port = 59125 self.__voice = 'dfki-prudence-hsmm' self.__voice_selections = 'dfki-prudence-hsmm en_GB female hmm' self.__logger = logging.getLogger('Borg.Brain.Util.MaryTTS') self.__logger.addHandler(nullhandler.NullHandler()) self.__available_voices = None self.__set_default_parameters() self.__request_content_type = None
def __init__(self): """ Set up the IOManager thread and start it. """ super(_IOManager, self).__init__() self.__poll = iopoll.IOPoll() self.__et = self.__poll.set_edge_triggered(True) self.__wrappers = {} self.__disconnected_wrappers = [] self.__running = True self.__wrapper_lock = threading.RLock() self.__poll_lock = threading.RLock() self.__logger = logging.getLogger('Borg.Brain.Util.IOManager') self.__logger.addHandler(nullhandler.NullHandler()) self.__empty_time = time.time() + 10 self.__next_tick = time.time() + 0.1 self.__parent_thread = threading.current_thread() self.__read_list = set([]) self.__write_list = set([]) self.__ticker = Ticker(10) self.__saved_time = 0 # Set up wake up pipe in non-blocking mode self.__wakeup_read, self.__wakeup_write = os.pipe() fl = fcntl.fcntl(self.__wakeup_read, fcntl.F_GETFL) fcntl.fcntl(self.__wakeup_read, fcntl.F_SETFL, fl | os.O_NONBLOCK) fl = fcntl.fcntl(self.__wakeup_write, fcntl.F_GETFL) fcntl.fcntl(self.__wakeup_write, fcntl.F_SETFL, fl | os.O_NONBLOCK) self.__poll.register(self.__wakeup_read, True, False, True) # Start IOManager thread self.start()
def __init__( self, host="localhost", # The host to connect to or serve port=49152, # The port to connect to or serve giveup=5, # Amount of reconnect attempts use_socket=None, # Pass in a connected socket object to use retry_timeout=1, # Timeout between connection attempts server=None, # True for a server, False for clients use_pickle=True, # Whether to pickle/unpickle # send/received data bufsize="auto", # Number of bytes to send/receive at once linger=.1, # Whether to set the SO_LINGER option handler=False, # Set to true to make this a incoming # connection handler logger=None # Pass in a logger to use ): """ Initialize the socket. The parameters define the behavior of the socket. Host should be an IP address or hostname to which to connect or on which to listen. If the server argument is not specifically set, the hostname will also define whether this will be a server or client socket: if host is empty, it will be a server socket listening on all interfaces, otherwise it will be a client socket. port should be the port to listen on. When the port is numeric it is assumed to be a TCP port. When it is a string, it will be cast to an int specifying a TCP port. If the conversion failed, it is assumed to be a Unix Socket, located in /tmp. This will raise an exception if the socket is trying to connect to a remove Unix Socket, which is impractical and defeats the purpose. giveup is the amount of retries performed to connect to a socket or bind to a port. use_socket can be used to pass in an already connected socket. The same socket options will be set on this socket as newly created ones. retry_timeout defines the number of seconds to wait between connection/ binding attempts of unconnected sockets. server is a boolean that specifies whether this will be a server or a client socket. A server socket will bind and listen to a port, a client socket will attempt to connect to a port. The default value decides this automatically, based on the value of the host parameter as described above. use_pickle is a boolean specifying whether communication on this socket will proceed using the Pickle protocol. If set to true, all outgoing data will be pickled and all incoming data will be unpickled. bufsize specifies the amount of data to send or receive at once. The default is auto, meaning it will be set depending on other options. When the socket to use is a Unix Domain Socket, it will use 128 KiB. If it is a client socket connecting to a local socket, it will be a 16 KiB and if it is a server socket or a client socket connecting to a non-local host, a buffer of 8 KiB is used. linger is a number specifying whether the socket will be set in linger mode (see man page for setsockopt(2)). When in linger mode, the call to close on the socket will block for at most a set number of seconds and will then forcefully close the socket. Otherwise, the close call is non-blocking and the socket will get in a TIME_WAIT state, which effectively blocks the port for up to 4 minutes, possibly resulting in 'Port already bound' messages. If the linger parameter is 0, LINGER will not be used; otherwise this is the amount of seconds to set. handler can be set to True to activate incoming connection handler behavior. This basically makes sure that no reconnetion attempt is performed when the connection is closed by the other end. """ self.__socket = None self.__fileno = None self.__client_sockets = [] self.__connected = False self.__listener = False self.__handler = handler self.__use_pickle = use_pickle self.__send_lock = threading.RLock() self.__receive_lock = threading.RLock() self.__receive_buffer = [] self.__recv_buffer = "" self.__send_buffer = [] self.__send_buffer_ids = set([]) self.__buffer_start_pos = 0 self.__send_id = 0 self.__set_port(host, port) self.__set_buffer_size(bufsize) self.__linger = float(linger) self.__total_bytes = 0 self.__log_input = False self.__log_output = False if server is None and self.__host == "": self.__listener = True else: self.__listener = server if logger: self.__logger = logger else: self.__logger = logging.getLogger('Borg.Brain.Util.ThreadedSocket') self.__logger.addHandler(nullhandler.NullHandler()) self.__last_attempt = 0 self.__timeout = retry_timeout self.__giveup = giveup self.__attempt = 0 if use_socket: self.__socket = use_socket self.__fileno = self.__socket.fileno() self.__port = port self.__connected = True self.__set_socket_options(self.__socket) mgr = IOManager() mgr.register(self) else: mgr = IOManager() mgr.register_unconnected(self)
import time import logging import nullhandler import loggingextra logging.getLogger('Borg.Brain.Util.Timer').addHandler( nullhandler.NullHandler()) class Timer(object): """ The Timer class can be used to do timeout checks in various settings, mainly behaviors. The main usage will be the use of the class method check, which will obtain a Timer from the repository with the specified name and check if it finished yet. If the timer was not used before, it will be instantiated. The class can also be used standalone, in that case the methods _check and _reset should be used instead. """ __registry = {} """ Check whether the timer with the given name has finished yet. The duration should be provided to instantiate the timer if this had not been done before. """ @classmethod def check(cls, name, duration): #print "Created timer: ", name t = cls.get(name, duration) return t._check()
To simplify the usage of the different polling mechanisms, the IOPoll class can be used. Simply instantiate an IOPoll() object and the best available mechanism is used. Using the poll method with a timeout in milliseconds, the respective poll function of the selected mechanism is invoked and the events are returns in a standardized format as either IO_READ, IO_WRITE or IO_ERROR, or a combination of those. Optionally, an argument specifiying a preference may be passed to IOPoll which will try to use the specified polling mechanism. You can specify, epoll, poll, kqueue or select, but the success depends on the availablility of the mechanism. """ IO_READ = 1 IO_WRITE = 2 IO_ERROR = 4 io_logger = logging.getLogger('Borg.Brain.Util.IOPoll') io_logger.addHandler(nullhandler.NullHandler()) _poll_class = None def IOPoll(pref=None): """ This function is a wrapper around the _IOPoll_xxxx classes. It selects the best polling mechnism available or uses the specified preference. It will then instantiate a polling object of the selected class (_IOPoll_epoll, _IOPoll_poll, _IOPoll_kqueue or _IOPoll_select). """ global _poll_class if pref: # A preference has been given if pref == "epoll":