def setUp(self): DefaultTestFixture.setUp(self) self.lock = Lock() self.pool = ThreadPool(3) self.queue = Queue() self.simple_action_counter = 0 return
def start(self): self.bPollRun = True if not self.work_queue: self.work_queue = ThreadPool(5) else: self.work_queue.resize(5) # Superclass calls run() on separate thread: return super(RNA_Scan_Thread, self).start()
class NBMManager(CompositeNode): """ NBMManager is the class used to manage remote hosts (NBM's). Operations that are queued on it are delegated to a threadpool for execution. A typical operation would be to ping or to check if the framework is running on the remote host. The NBMManager can also be used to obtain a (rna) reference to a remote NBM. This is an inherent service running on the NBM-Manager. """ def __init__(self): self._hosts = {} self._running = False self._scheduled = None # Default value for Managed Hosts # to use Managed Proxies. self.manage_proxies = True self._tp = ThreadPool(THREADPOOLSIZE, name='NBMManagerThread') super(NBMManager, self).__init__() def configure(self, config): set_attribute(self, 'manage_proxies', True, config, as_boolean) return super(NBMManager, self).configure(config) def configuration(self): config = super(NBMManager, self).configuration() get_attribute(self, 'manage_proxies', config, str) return config def start(self): for host in self.children_nodes(): self._hosts[host.name] = host self._running = True return super(NBMManager, self).start() def stop(self): self._running = False return super(NBMManager, self).stop() def get_host(self, hostname): host = self._hosts.get(hostname, None) if host is None and self.has_child(hostname): host = self.get_child(hostname) self._hosts[host.name] = host else: for h in self._hosts.values(): if h.host == hostname: host = h break if host is None: err_msg = 'Invalid configuration: NBM Manager is missing host %s' \ % hostname raise EConfiguration(err_msg) return host def queue(self, callback, args=()): self._tp.queue_noresult(callback, *args) def schedule(self, delay, callback, args=()): return scheduler.after(delay, self.queue, (callback, args))
def __init__(self): self._hosts = {} self._running = False self._scheduled = None # Default value for Managed Hosts # to use Managed Proxies. self.manage_proxies = True self._tp = ThreadPool(THREADPOOLSIZE, name='NBMManagerThread') super(NBMManager, self).__init__()
class CommandManager(object): def __init__(self): super(CommandManager, self).__init__() self._worker_thread = ThreadPool( 2, name='GlobalSetpointManager-ThreadPool') self._transactions = Cache(size=20) def enqueue(self, command_set): self._transactions[command_set.tid] = command_set self._worker_thread.queue_noresult(command_set) def get_push_values_progress(self, transaction_id): try: return self._transactions[transaction_id].get_status() except KeyError: raise ENotFound() def singleton_unload_hook(self): pass
class CommandManager(object): def __init__(self): super(CommandManager, self).__init__() self._worker_thread = ThreadPool( 2, name='GlobalSetpointManager-ThreadPool' ) self._transactions = Cache(size=20) def enqueue(self, command_set): self._transactions[command_set.tid] = command_set self._worker_thread.queue_noresult(command_set) def get_push_values_progress(self, transaction_id): try: return self._transactions[transaction_id].get_status() except KeyError: raise ENotFound() def singleton_unload_hook(self): pass
def __init__(self, timeout=2.0): self.timeout = timeout self.stations = {} self._monitor = monitor.ChannelMonitor(self.timeout) self.tm_number = self.tm_counter.increment() self._response_tp = ThreadPool(1, 'Jace Response Pool') self._pending_responses = Queue() self._callbacks = {} self._running = False self._sync_get_lock = Lock() self._last_sync_get = uptime.secs() self._cv = Condition() ImmortalThread.__init__(self, None, None, 'Jace Transaction Manager') return
class RNA_Scan_Thread(Thread): """ Polls for and handles any incoming msgs from established RNA sockets connected to clients.""" def __init__(self, name='RNA_Scan_Thread'): pfx = 'RNA_Scan_Thread.__init__:' msg = '%s Entering...' % pfx msglog.log('broadway', msglog.types.INFO, msg) self.debug = 0 # Maps file-descriptors to host-names. self.hosts = {} # Maps file-descriptors to sessions. self.sessions = {} # Maps host-names to sets of file-descriptors self.socketmap = {} self.connections = {} self.bPollRun = False self.work_queue = None self.descriptors = set() self.trigger_channel = Trigger(self.socketmap) self.descriptors.add(self.trigger_channel.fileno()) msg = '%s Done.' % pfx msglog.log('broadway', msglog.types.INFO, msg) super(RNA_Scan_Thread, self).__init__(name=name) def start(self): self.bPollRun = True if not self.work_queue: self.work_queue = ThreadPool(5) else: self.work_queue.resize(5) # Superclass calls run() on separate thread: return super(RNA_Scan_Thread, self).start() def stop(self): self.bPollRun = False self.notify_poll() self.work_queue.resize(0) return super(RNA_Scan_Thread, self).stop() def is_running(self): return self.isAlive() def run(self): pfx = 'RNA_Scan_Thread.run:' cmdfd = self.trigger_channel.fileno() enqueue = self.work_queue.queue_noresult handle_session = self.handle_session_input clear_notifications = self.clear_notifications while self.bPollRun: try: descriptors = self.descriptors.copy() if cmdfd not in descriptors: msglog.warn("Command channel FD removed!") descriptors.add(cmdfd) r,w,e = select.select(descriptors, [], descriptors, 1) for fd in e: if fd == cmdfd: message = "%s internal polling error. Must restart." msglog.error(message % pfx) raise TypeError("command channel OOB data") try: self.unregister_session(self.get_session(fd)) except: msglog.warn("%s I/O event handling error." % pfx) msglog.exception(prefix="handled") for fd in r: if fd in self.descriptors: try: if fd == cmdfd: clear_notifications() else: self.descriptors.discard(fd) enqueue(handle_session, fd) except: msglog.warn("%s I/O event handling error." % pfx) msglog.exception(prefix="handled") except: msglog.warn("%s loop error." % pfx) msglog.exception(prefix="handled") msglog.inform("%s exiting." % pfx) def notify_poll(self): self.trigger_channel.trigger_event() def clear_notifications(self): self.trigger_channel.handle_read() def register_session(self, session): try: fd = session.socket.fileno() host,port = session.socket.getpeername() except: msglog.warn("Failed to add session: %r" % session) msglog.exception(prefix="handled") registered = False else: self.hosts[fd] = host self.sessions[fd] = session self.connections.setdefault(host, set()).add(fd) self.descriptors.add(fd) self.notify_poll() registered = True return registered def unregister_session(self, session): try: fd = session.socket.fileno() except: unregistered = False else: host = self.hosts.pop(fd, None) if host is not None: connections = self.connections.get(host, None) if connections is not None: connections.discard(fd) if not connections: self.connections.pop(host, None) self.sessions.pop(fd, None) if fd in self.descriptors: self.descriptors.discard(fd) self.notify_poll() try: session.disconnect() except: msglog.exception(prefix="handled") unregistered = True return unregistered def get_session(self, fd): if not isinstance(fd, int): fd = fd.fileno() return self.sessions[fd] def get_host(self, fd): if not isinstance(fd, int): fd = fd.fileno() return self.hosts[fd] def get_connections(self, host): return self.connections.get(host, []) def has_connections(self, host): return host in self.connections def get_sessions(self, host): connections = self.get_connections(host) return map(self.get_session, connections) def handle_session_input(self, fd): pfx = 'RNA_Scan_Thread.handle_session_input' unregister = False session = self.get_session(fd) try: rna_header = RNAHeader(session.socket) protocol = _protocol_factory(rna_header.protocol) protocol.setup(session, rna_header) command = protocol.recv_command() result = _invoke_command(command) protocol.send_result(command, result) except EInvalidMessage, error: if not error.message: unregister = True msglog.warn("Removing client-closed session: %s" % session) else: unregister = False msglog.exception(prefix="handled") except SocketTimeoutError: unregister = False message.exception(prefix="handled")
class TestCase(DefaultTestFixture): def setUp(self): DefaultTestFixture.setUp(self) self.lock = Lock() self.pool = ThreadPool(3) self.queue = Queue() self.simple_action_counter = 0 return def tearDown(self): self.pool._unload() DefaultTestFixture.tearDown(self) return def simple_action(self, object): # @note It appears that even the '+= 1' operation is not # guaranteed to be atomic. self.lock.acquire() self.simple_action_counter += 1 self.lock.release() return 'simple_action_result' def slow_action(self, object): time.sleep(1.0) return 'slow_action_result' def simple_queue_action(self, object): self.queue.put(object) return def test_simple_queue(self): self.pool.queue(self.simple_queue_action, self) result = self.queue.get(1.0) if result is not self: raise "Queue returned %r instead of self, %r." % (result, self) return def test_result(self): t1 = time.time() pending_result = self.pool.queue(self.simple_action, self) result = pending_result.result(10.0) t2 = time.time() if result != 'simple_action_result': raise ("pending_result.result() returned the wrong value (%s)." % result) if (t2 - t1) >= 10.0: raise "pending_result.result() blocked for no reason." return def test_pending_reasult(self): t1 = time.time() pending_result = PendingResult(None, None, self.simple_action, self) pending_result_two = self.pool.queue_pending_result(pending_result) if pending_result_two is not pending_result: raise "pending_result_two is NOT pending_result" result = pending_result.result(10.0) t2 = time.time() if result != 'simple_action_result': raise ("pending_result.result() returned the wrong value (%s)." % result) if (t2 - t1) >= 10.0: raise "pending_result.result() blocked for no reason." return def test_pending_action(self): pending_action = PendingAction(self.simple_queue_action, self) self.pool.queue_pending_action(pending_action) result = self.queue.get(1.0) if result is not self: raise "Queue returned %r instead of self, %r." % (result, self) return return def test_result_timeout(self): t1 = time.time() pending_result = self.pool.queue(self.slow_action, self) result = pending_result.result(0.25) t2 = time.time() if (t2 - t2) >= 1.0: raise "Blocked 1 second when a 1/4 second timeout." if result != NORESULT: raise "Got a result (%s) when none was expected." return def test_1000_actions(self): for i in xrange(0, 1000): self.pool.queue(self.simple_action, self) time.sleep(0.1) t1 = time.time() while self.simple_action_counter < 1000: tn = time.time() if (tn - t1) > 3.0: raise ( "Taking ridiculously long to process 1000 queued actions.") time.sleep(0.1) return def test_HIGH_pool_1(self): t1 = time.time() pending_result = HIGH.queue(self.simple_action, self) result = pending_result.result(10.0) t2 = time.time() if result != 'simple_action_result': raise ("pending_result.result() returned the wrong value (%s)." % result) if (t2 - t1) >= 10.0: raise "pending_result.result() blocked for no reason." return def test_HIGH_pool_2(self): self.test_HIGH_pool_1() return def test_HIGH_pool_resize_1(self): HIGH.resize(1) if HIGH.size() != 1: raise "Resize to 1 thread failed." for i in xrange(0, 100): HIGH.queue(self.simple_action, self) t1 = time.time() while self.simple_action_counter < 100: tn = time.time() if (tn - t1) > 3.0: raise ( "Taking ridiculously long to process 100 queued actions.") time.sleep(0.1) return def test_HIGH_pool_resize_20(self): HIGH.resize(20) if HIGH.size() != 20: raise "Resize to 20 threads failed." for i in xrange(0, 100): HIGH.queue(self.simple_action, self) t1 = time.time() while self.simple_action_counter < 100: tn = time.time() if (tn - t1) > 3.0: raise ( "Taking ridiculously long to process 100 queued actions.") time.sleep(0.1) return
def __init__(self): super(CommandManager, self).__init__() self._worker_thread = ThreadPool( 2, name='GlobalSetpointManager-ThreadPool' ) self._transactions = Cache(size=20)
from mpx.lib.configure import map_to_attribute, map_from_attribute from mpx.lib.configure import map_to_seconds, map_from_seconds, as_boolean from mpx.lib.event import ConnectionEvent, EventConsumerMixin from mpx.lib.exceptions import EAlreadyRunning, ENoData, EBreakupTransfer, \ ENotEnabled,ETimeout,EConnectionError, ENoSuchName, EInvalidValue, EIOError from mpx.lib.node import as_node, as_node_url from mpx.lib.scheduler import scheduler from mpx.lib.persistent import PersistentDataObject from mpx.lib.threading import Lock from mpx.lib.thread_pool import ThreadPool from _exporter import Exporter from _formatter_exceptions import EInconsistantFormat, EIncompatiableFormat from _transporter_exceptions import ETransporter Exporter_ThreadPool = ThreadPool(2, name='PeriodicExporter-ThreadPool') def _days_to_seconds(days): return float(days) * 86400 def _seconds_to_days(seconds): return str(seconds / 86400) class _TimeStore(PersistentDataObject): def __init__(self, node): self.last_time = 0 PersistentDataObject.__init__(self, node)
class RNA_Scan_Thread(Thread): """ Polls for and handles any incoming msgs from established RNA sockets connected to clients.""" def __init__(self, name='RNA_Scan_Thread'): pfx = 'RNA_Scan_Thread.__init__:' msg = '%s Entering...' % pfx msglog.log('broadway', msglog.types.INFO, msg) self.debug = 0 # Maps file-descriptors to host-names. self.hosts = {} # Maps file-descriptors to sessions. self.sessions = {} # Maps host-names to sets of file-descriptors self.socketmap = {} self.connections = {} self.bPollRun = False self.work_queue = None self.descriptors = set() self.trigger_channel = Trigger(self.socketmap) self.descriptors.add(self.trigger_channel.fileno()) msg = '%s Done.' % pfx msglog.log('broadway', msglog.types.INFO, msg) super(RNA_Scan_Thread, self).__init__(name=name) def start(self): self.bPollRun = True if not self.work_queue: self.work_queue = ThreadPool(5) else: self.work_queue.resize(5) # Superclass calls run() on separate thread: return super(RNA_Scan_Thread, self).start() def stop(self): self.bPollRun = False self.notify_poll() self.work_queue.resize(0) return super(RNA_Scan_Thread, self).stop() def is_running(self): return self.isAlive() def run(self): pfx = 'RNA_Scan_Thread.run:' cmdfd = self.trigger_channel.fileno() enqueue = self.work_queue.queue_noresult handle_session = self.handle_session_input clear_notifications = self.clear_notifications while self.bPollRun: try: descriptors = self.descriptors.copy() if cmdfd not in descriptors: msglog.warn("Command channel FD removed!") descriptors.add(cmdfd) r, w, e = select.select(descriptors, [], descriptors, 1) for fd in e: if fd == cmdfd: message = "%s internal polling error. Must restart." msglog.error(message % pfx) raise TypeError("command channel OOB data") try: self.unregister_session(self.get_session(fd)) except: msglog.warn("%s I/O event handling error." % pfx) msglog.exception(prefix="handled") for fd in r: if fd in self.descriptors: try: if fd == cmdfd: clear_notifications() else: self.descriptors.discard(fd) enqueue(handle_session, fd) except: msglog.warn("%s I/O event handling error." % pfx) msglog.exception(prefix="handled") except: msglog.warn("%s loop error." % pfx) msglog.exception(prefix="handled") msglog.inform("%s exiting." % pfx) def notify_poll(self): self.trigger_channel.trigger_event() def clear_notifications(self): self.trigger_channel.handle_read() def register_session(self, session): try: fd = session.socket.fileno() host, port = session.socket.getpeername() except: msglog.warn("Failed to add session: %r" % session) msglog.exception(prefix="handled") registered = False else: self.hosts[fd] = host self.sessions[fd] = session self.connections.setdefault(host, set()).add(fd) self.descriptors.add(fd) self.notify_poll() registered = True return registered def unregister_session(self, session): try: fd = session.socket.fileno() except: unregistered = False else: host = self.hosts.pop(fd, None) if host is not None: connections = self.connections.get(host, None) if connections is not None: connections.discard(fd) if not connections: self.connections.pop(host, None) self.sessions.pop(fd, None) if fd in self.descriptors: self.descriptors.discard(fd) self.notify_poll() try: session.disconnect() except: msglog.exception(prefix="handled") unregistered = True return unregistered def get_session(self, fd): if not isinstance(fd, int): fd = fd.fileno() return self.sessions[fd] def get_host(self, fd): if not isinstance(fd, int): fd = fd.fileno() return self.hosts[fd] def get_connections(self, host): return self.connections.get(host, []) def has_connections(self, host): return host in self.connections def get_sessions(self, host): connections = self.get_connections(host) return map(self.get_session, connections) def handle_session_input(self, fd): pfx = 'RNA_Scan_Thread.handle_session_input' unregister = False session = self.get_session(fd) try: rna_header = RNAHeader(session.socket) protocol = _protocol_factory(rna_header.protocol) protocol.setup(session, rna_header) command = protocol.recv_command() result = _invoke_command(command) protocol.send_result(command, result) except EInvalidMessage, error: if not error.message: unregister = True msglog.warn("Removing client-closed session: %s" % session) else: unregister = False msglog.exception(prefix="handled") except SocketTimeoutError: unregister = False message.exception(prefix="handled")
class TestCase(DefaultTestFixture): def setUp(self): DefaultTestFixture.setUp(self) self.lock = Lock() self.pool = ThreadPool(3) self.queue = Queue() self.simple_action_counter = 0 return def tearDown(self): self.pool._unload() DefaultTestFixture.tearDown(self) return def simple_action(self, object): # @note It appears that even the '+= 1' operation is not # guaranteed to be atomic. self.lock.acquire() self.simple_action_counter += 1 self.lock.release() return 'simple_action_result' def slow_action(self, object): time.sleep(1.0) return 'slow_action_result' def simple_queue_action(self, object): self.queue.put(object) return def test_simple_queue(self): self.pool.queue(self.simple_queue_action, self) result = self.queue.get(1.0) if result is not self: raise "Queue returned %r instead of self, %r." % (result, self) return def test_result(self): t1 = time.time() pending_result = self.pool.queue(self.simple_action, self) result = pending_result.result(10.0) t2 = time.time() if result != 'simple_action_result': raise ( "pending_result.result() returned the wrong value (%s)." % result ) if (t2-t1) >= 10.0: raise "pending_result.result() blocked for no reason." return def test_pending_reasult(self): t1 = time.time() pending_result = PendingResult(None, None, self.simple_action, self) pending_result_two = self.pool.queue_pending_result(pending_result) if pending_result_two is not pending_result: raise "pending_result_two is NOT pending_result" result = pending_result.result(10.0) t2 = time.time() if result != 'simple_action_result': raise ( "pending_result.result() returned the wrong value (%s)." % result ) if (t2-t1) >= 10.0: raise "pending_result.result() blocked for no reason." return def test_pending_action(self): pending_action = PendingAction(self.simple_queue_action, self) self.pool.queue_pending_action(pending_action) result = self.queue.get(1.0) if result is not self: raise "Queue returned %r instead of self, %r." % (result, self) return return def test_result_timeout(self): t1 = time.time() pending_result = self.pool.queue(self.slow_action, self) result = pending_result.result(0.25) t2 = time.time() if (t2-t2) >= 1.0: raise "Blocked 1 second when a 1/4 second timeout." if result != NORESULT: raise "Got a result (%s) when none was expected." return def test_1000_actions(self): for i in xrange(0,1000): self.pool.queue(self.simple_action, self) time.sleep(0.1) t1 = time.time() while self.simple_action_counter < 1000: tn = time.time() if (tn-t1) > 3.0: raise ( "Taking ridiculously long to process 1000 queued actions." ) time.sleep(0.1) return def test_HIGH_pool_1(self): t1 = time.time() pending_result = HIGH.queue(self.simple_action, self) result = pending_result.result(10.0) t2 = time.time() if result != 'simple_action_result': raise ( "pending_result.result() returned the wrong value (%s)." % result ) if (t2-t1) >= 10.0: raise "pending_result.result() blocked for no reason." return def test_HIGH_pool_2(self): self.test_HIGH_pool_1() return def test_HIGH_pool_resize_1(self): HIGH.resize(1) if HIGH.size() != 1: raise "Resize to 1 thread failed." for i in xrange(0,100): HIGH.queue(self.simple_action, self) t1 = time.time() while self.simple_action_counter < 100: tn = time.time() if (tn-t1) > 3.0: raise ( "Taking ridiculously long to process 100 queued actions." ) time.sleep(0.1) return def test_HIGH_pool_resize_20(self): HIGH.resize(20) if HIGH.size() != 20: raise "Resize to 20 threads failed." for i in xrange(0,100): HIGH.queue(self.simple_action, self) t1 = time.time() while self.simple_action_counter < 100: tn = time.time() if (tn-t1) > 3.0: raise ( "Taking ridiculously long to process 100 queued actions." ) time.sleep(0.1) return
def __init__(self): super(CommandManager, self).__init__() self._worker_thread = ThreadPool( 2, name='GlobalSetpointManager-ThreadPool') self._transactions = Cache(size=20)