def write_lock(self): me = tu.get_ident() if self.is_reader(): raise RuntimeError("Reader %s to writer privilege" " escalation not allowed" % me) if self.is_writer(check_pending=False): # Already the writer; this allows for basic reentrancy. yield self else: self._cond.acquire() try: self._pending_writers.append(me) while True: # No readers, and no active writer, am I next?? if len(self._readers) == 0 and self._writer is None: if self._pending_writers[0] == me: self._writer = self._pending_writers.popleft() break self._cond.wait() finally: self._cond.release() try: yield self finally: self._cond.acquire() try: self._writer = None self._cond.notify_all() finally: self._cond.release()
def is_reader(self): """Returns if the caller is one of the readers.""" self._cond.acquire() try: return tu.get_ident() in self._readers finally: self._cond.release()
def read_lock(self): me = tu.get_ident() if self.is_writer(): raise RuntimeError("Writer %s can not acquire a read lock" " while holding/waiting for the write lock" % me) self._cond.acquire() try: while True: # No active writer; we are good to become a reader. if self._writer is None: self._readers.append(me) break # An active writer; guess we have to wait. self._cond.wait() finally: self._cond.release() try: yield self finally: # I am no longer a reader, remove *one* occurrence of myself. # If the current thread acquired two read locks, then it will # still have to remove that other read lock; this allows for # basic reentrancy to be possible. self._cond.acquire() try: self._readers.remove(me) self._cond.notify_all() finally: self._cond.release()
def is_writer(self, check_pending=True): self._cond.acquire() try: me = tu.get_ident() if self._writer is not None and self._writer == me: return True if check_pending: return me in self._pending_writers else: return False finally: self._cond.release()
def is_writer(self, check_pending=True): """Returns if the caller is the active writer or a pending writer.""" self._cond.acquire() try: me = tu.get_ident() if self._writer is not None and self._writer == me: return True if check_pending: return me in self._pending_writers else: return False finally: self._cond.release()
def _generate_banner(self): """Generates a banner that can be useful to display before running.""" try: tpl_params = dict(self.BANNER_TEMPLATE.defaults) except AttributeError: tpl_params = {} connection_details = self._server.connection_details transport = connection_details.transport if transport.driver_version: transport_driver = "%s v%s" % (transport.driver_name, transport.driver_version) else: transport_driver = transport.driver_name tpl_params['transport_driver'] = transport_driver tpl_params['exchange'] = self._exchange tpl_params['topic'] = self._topic tpl_params['transport_type'] = transport.driver_type tpl_params['connection_uri'] = connection_details.uri tpl_params['executor_type'] = reflection.get_class_name(self._executor) threads_count = getattr(self._executor, 'max_workers', None) if threads_count is not None: tpl_params['executor_thread_count'] = threads_count if self._endpoints: pretty_endpoints = [] for ep in self._endpoints: pretty_endpoints.append(" - %s" % ep) # This ensures there is a newline before the list... tpl_params['endpoints'] = "\n" + "\n".join(pretty_endpoints) try: tpl_params['hostname'] = socket.getfqdn() except socket.error: pass try: tpl_params['pid'] = os.getpid() except OSError: pass tpl_params['platform'] = platform.platform() tpl_params['thread_id'] = tu.get_ident() banner = self.BANNER_TEMPLATE.substitute(**tpl_params) # NOTE(harlowja): this is needed since the template in this file # will always have newlines that end with '\n' (even on different # platforms due to the way this source file is encoded) so we have # to do this little dance to make it platform neutral... return misc.fix_newlines(banner)
def banner(self): """A banner that can be useful to display before running.""" connection_details = self._server.connection_details transport = connection_details.transport if transport.driver_version: transport_driver = "%s v%s" % (transport.driver_name, transport.driver_version) else: transport_driver = transport.driver_name try: hostname = socket.getfqdn() except socket.error: hostname = "???" try: pid = os.getpid() except OSError: pid = "???" chapters = { 'Connection details': { 'Driver': transport_driver, 'Exchange': self._exchange, 'Topic': self._topic, 'Transport': transport.driver_type, 'Uri': connection_details.uri, }, 'Powered by': { 'Executor': reflection.get_class_name(self._executor), 'Thread count': getattr(self._executor, 'max_workers', "???"), }, 'Supported endpoints': [str(ep) for ep in self._endpoints], 'System details': { 'Hostname': hostname, 'Pid': pid, 'Platform': platform.platform(), 'Python': sys.version.split("\n", 1)[0].strip(), 'Thread id': tu.get_ident(), }, } return banner.make_banner('WBE worker', chapters)
def banner(self): """A banner that can be useful to display before running.""" connection_details = self._server.connection_details transport = connection_details.transport if transport.driver_version: transport_driver = "%s v%s" % (transport.driver_name, transport.driver_version) else: transport_driver = transport.driver_name try: hostname = socket.getfqdn() except socket.error: hostname = "???" try: pid = os.getpid() except OSError: pid = "???" chapters = { "Connection details": { "Driver": transport_driver, "Exchange": self._exchange, "Topic": self._topic, "Transport": transport.driver_type, "Uri": connection_details.uri, }, "Powered by": { "Executor": reflection.get_class_name(self._executor), "Thread count": getattr(self._executor, "max_workers", "???"), }, "Supported endpoints": [str(ep) for ep in self._endpoints], "System details": { "Hostname": hostname, "Pid": pid, "Platform": platform.platform(), "Python": sys.version.split("\n", 1)[0].strip(), "Thread id": tu.get_ident(), }, } return banner.make_banner("WBE worker", chapters)
def _generate_banner(self): """Generates a banner that can be useful to display before running.""" tpl_params = {} connection_details = self._server.connection_details transport = connection_details.transport if transport.driver_version: transport_driver = "%s v%s" % (transport.driver_name, transport.driver_version) else: transport_driver = transport.driver_name tpl_params['transport_driver'] = transport_driver tpl_params['exchange'] = self._exchange tpl_params['topic'] = self._topic tpl_params['transport_type'] = transport.driver_type tpl_params['connection_uri'] = connection_details.uri tpl_params['executor_type'] = reflection.get_class_name(self._executor) if self._threads_count != -1: tpl_params['executor_thread_count'] = self._threads_count if self._endpoints: pretty_endpoints = [] for ep in self._endpoints: pretty_endpoints.append(" - %s" % ep) # This ensures there is a newline before the list... tpl_params['endpoints'] = "\n" + "\n".join(pretty_endpoints) try: tpl_params['hostname'] = socket.getfqdn() except socket.error: pass try: tpl_params['pid'] = os.getpid() except OSError: pass tpl_params['platform'] = platform.platform() tpl_params['thread_id'] = tu.get_ident() return BANNER_TEMPLATE.substitute(BANNER_TEMPLATE.defaults, **tpl_params)
def write_lock(self): """Context manager that grants a write lock. Will wait until no active readers. Blocks readers after acquiring. Raises a RuntimeError if an active reader attempts to acquire a lock. """ me = tu.get_ident() if self.is_reader(): raise RuntimeError("Reader %s to writer privilege" " escalation not allowed" % me) if self.is_writer(check_pending=False): # Already the writer; this allows for basic reentrancy. yield self else: self._cond.acquire() try: self._pending_writers.append(me) while True: # No readers, and no active writer, am I next?? if len(self._readers) == 0 and self._writer is None: if self._pending_writers[0] == me: self._writer = self._pending_writers.popleft() break self._cond.wait() finally: self._cond.release() try: yield self finally: self._cond.acquire() try: self._writer = None self._cond.notify_all() finally: self._cond.release()
def is_reader(self): self._cond.acquire() try: return tu.get_ident() in self._readers finally: self._cond.release()
def execute(self): print("Running '%s' in thread '%s'" % (self.name, tu.get_ident())) time.sleep(self._wait_for)