def connect(self, name: str, ns_host: str = None, ns_port: float = None) -> None: """ Connect to a remote PyroLab-hosted UC480 camera. Assumes the nameserver where the camera is registered is already configured in the environment. Parameters ---------- name : str The name used to register the camera on the nameserver. Examples -------- >>> from pyrolab.api import NameServerConfiguration >>> from pyrolab.drivers.cameras.thorcam import ThorCamClient >>> nscfg = NameServerConfiguration(host="my.nameserver.com") >>> nscfg.update_pyro_config() >>> cam = ThorCamClient() >>> cam.connect("camera_name") """ if ns_host or ns_port: args = {'host': ns_host, 'port': ns_port} else: args = {} with locate_ns(**args) as ns: self.cam = Proxy(ns.lookup(name)) self.cam.autoconnect() self.remote_attributes = self.cam._pyroAttrs self._LOCAL_HEADERSIZE = self.HEADERSIZE
def proxy(self): try: proxy = Proxy(self.uri) proxy._pyroTimeout = self.pyro_timeout return proxy except Exception as e: logger.error("Pyro5 proxy error: {}".format(e))
def main(): dispatcher = Proxy("PYRONAME:example.distributed.dispatcher") print("This is worker %s" % WORKERNAME) print("getting work from dispatcher.") while True: try: item = dispatcher.getWork() except ValueError: print("no work available yet.") else: process(item) dispatcher.putResult(item)
def process(self, message): if self.next is None: self.next = Proxy("PYRONAME:example.chain." + self.nextName) if self.name in message: print("Back at %s; we completed the circle!" % self.name) return ["complete at " + self.name] else: print("I'm %s, passing to %s" % (self.name, self.nextName)) message.append(self.name) result = self.next.process(message) result.insert(0, "passed on from " + self.name) return result
def Adjust(self, state): with (self.m_lock): self.m_currentCompare = self.m_currentCompare + state if (SSRC.PWM.LIMIT_MIN > self.m_currentCompare): self.m_currentCompare = SSRC.PWM.LIMIT_MIN if (SSRC.PWM.LIMIT_MAX < self.m_currentCompare): self.m_currentCompare = SSRC.PWM.LIMIT_MAX self.m_threadCondition.acquire() self.m_threadCondition.notify() self.m_threadCondition.release() kasaObj = Proxy(("PYRO:{}@{}:{}").format(KASA.DAEMON.PYRO_OBJECT_ID, KASA.DAEMON.PYRO_HOST, KASA.DAEMON.PYRO_PORT)) kasaObj.TurnPlugOn()
def Exit(self): with self.m_lock: self.m_active = False self.m_threadCondition.acquire() self.m_threadCondition.notify() self.m_threadCondition.release() self.m_thread.join() kasaObj = Proxy(("PYRO:{}@{}:{}").format(KASA.DAEMON.PYRO_OBJECT_ID, KASA.DAEMON.PYRO_HOST, KASA.DAEMON.PYRO_PORT)) kasaObj.TurnPlugOff() kasaObj.Exit() logging.debug("Closing socket") self.m_daemon.shutdown()
def all_subscribers(self): conn = self.dbconn() with closing(conn.cursor()) as cursor: result = cursor.execute( "SELECT s.id, t.topic, s.subscriber FROM Subscription AS s, Topic AS t WHERE t.id=s.topic" ).fetchall() subs = defaultdict(list) for sub_id, topic, uri in result: if uri in self.proxy_cache: proxy = self.proxy_cache[uri] subs[topic].append(proxy) else: try: proxy = Proxy(uri) except Exception: log.exception( "Cannot create pyro proxy, sub_id=%d, uri=%s", sub_id, uri) cursor.execute("DELETE FROM Subscription WHERE id=?", [sub_id]) else: self.proxy_cache[uri] = proxy subs[topic].append(proxy) conn.commit() return subs
def __init__(self, auto_consume=True, max_queue_size=5000): self.bus = Proxy("PYRONAME:"+PYRO_MSGBUS_NAME) self.received_messages = queue.Queue(maxsize=max_queue_size) if auto_consume: self.__bus_consumer_thread = threading.Thread(target=self.__bus_consume_message) self.__bus_consumer_thread.daemon = True self.__bus_consumer_thread.start()
def get_logs_in_timeframe(timeframe): start = timeframe[0] end = timeframe[1] logs_folder = '' with Proxy('PYRONAME:serial_server.serial_connection') as controller: logs_folder = os.path.join(controller.working_directory, 'logs', '*.csv') all_logs = glob.glob(logs_folder) all_logs.sort(reverse=True) valid_logs = [] for log in all_logs: log_date_string = re.search( '(\d{4}\.\d{2}\.\d{2}_\d{2}\.\d{2}\.\d{2})', log) log_date = datetime.datetime.strptime(log_date_string[0], '%Y.%m.%d_%H.%M.%S') if log_date < end and log_date > start: if os.path.getsize(log) > 0: valid_logs.append(log) continue if log_date < start: if os.path.getsize(log) > 0: valid_logs.append(log) break valid_logs.sort() return valid_logs
def test(): with Proxy(uri) as serv: print("\nFIRST: no timezone") date1 = serv.echo(datetime.datetime.now()) print("{0}\n {1} ({2})".format(date1, repr(date1), type(date1))) if hasattr(date1, "tzinfo"): print(" tzinfo =", date1.tzinfo) else: print(" no tzinfo attribute") print("\nSECOND: PyTz timezones") date1 = serv.pytz() assert isinstance(date1.tzinfo, datetime.tzinfo) print("{0}\n {1} ({2})\n {3}".format(date1, date1.tzinfo, type(date1.tzinfo), date1.strftime(fmt))) date2 = serv.echo(date1) print("{0}\n {1} ({2})\n {3}".format(date2, date2.tzinfo, type(date2.tzinfo), date2.strftime(fmt))) assert date1 == date2 print("\nTHIRD: DateUtil timezones") date1 = serv.dateutil() assert isinstance(date1.tzinfo, datetime.tzinfo) print("{0}\n {1} ({2})\n {3}".format(date1, date1.tzinfo, type(date1.tzinfo), date1.strftime(fmt))) date2 = serv.echo(date1) print("{0}\n {1} ({2})\n {3}".format(date2, date2.tzinfo, type(date2.tzinfo), date2.strftime(fmt))) assert date1 == date2
def do_job(self, log_to_file=True): with Proxy('PYRONAME:serial_server.serial_connection' ) as serial_connection: filename = os.path.basename( serial_connection.connection_name ) + '_' + datetime.datetime.now().strftime( '%Y.%m.%d_%H.%M.%S') + '.csv' logs_folder = os.path.join(serial_connection.working_directory, 'logs') try: os.makedirs(logs_folder) except OSError: pass full_filepath = os.path.join(logs_folder, filename) while not self._exit.is_set(): serial_line = serial_connection.post_message('sr') serial_line = serial_line[:-2] timestamp_line = datetime.datetime.now().strftime('%x %X') print('Returned data: "' + timestamp_line + ',' + serial_line + '"') if log_to_file: if not os.path.exists(full_filepath): with open(full_filepath, 'x') as f: f.write('t,0,1,2\r\n') with open(full_filepath, 'a') as f: f.write(timestamp_line + "," + serial_line + '\r\n') delay = serial_connection.delay print('Sleeping for ' + str(delay)) self._exit.wait(delay)
def count(self, lines): # use the name server's prefix lookup to get all registered wordcounters with locate_ns() as ns: all_counters = ns.list(prefix="example.dc.wordcount.") counters = [Proxy(uri) for uri in all_counters.values()] #for c in counters: # c._pyroAsync() # set proxy in asynchronous mode # @todo alternative for ASYNC roundrobin_counters = cycle(counters) # chop the text into chunks that can be distributed across the workers # uses asynchronous proxy so that we can hand off everything in parallel # counter is selected in a round-robin fashion from list of all available counters # (This is a brain dead way because it doesn't scale - all the asynchronous calls are hogging # the worker threads in the server. That's why we've increased that a lot at the start # of this file, just for the sake of this example!) async_results = [] for chunk in grouper(100, lines): counter = next(roundrobin_counters) result = counter.count(chunk) async_results.append(result) # gather the results print("Collecting %d results..." % len(async_results)) totals = Counter() for result in async_results: try: totals.update(result) # @todo alternative for ASYNC results except Pyro5.errors.CommunicationError as x: raise Pyro5.errors.PyroError( "Something went wrong in the server when collecting the async responses: " + str(x)) for proxy in counters: proxy._pyroRelease() return totals
def __init__(self, config_name: str = "maestral", fallback: bool = False) -> None: self._config_name = config_name self._is_fallback = False if is_running(config_name): sock_name = sockpath_for_config(config_name) # print remote tracebacks locally sys.excepthook = Pyro5.errors.excepthook self._m = Proxy(URI.format(config_name, "./u:" + sock_name)) try: self._m._pyroBind() except CommunicationError: self._m._pyroRelease() raise else: # If daemon is not running, fall back to new Maestral instance # or raise a CommunicationError if fallback not allowed. if fallback: from .main import Maestral self._m = Maestral(config_name) else: raise CommunicationError( f"Could not get proxy for '{config_name}'") self._is_fallback = not isinstance(self._m, Proxy)
def get_proxy(self, tunnel, is_deco_proxy=False): tunnel_ip_ports_list = tunnel.local_bind_addresses local_ip, local_port = tunnel_ip_ports_list[0] if self.offline: if is_deco_proxy: logging.debug( 'Getting Pyro Proxy in offline mode -> Direct connection to device (deco)...' ) uri = 'PYRO:' + self.lineEdit_proxy_deco.text() + '@' + str( local_ip) + ':' + str(local_port) else: logging.debug( 'Getting Pyro Proxy in offline mode -> Direct connection to device (recorder)...' ) uri = 'PYRO:' + self.lineEdit_proxy.text() + '@' + str( local_ip) + ':' + str(local_port) else: logging.debug( 'Getting Pyro Proxy in standard mode -> Connection through Central Server...' ) uri = 'PYRO:' + self.config["central_server_proxy"] + '@' + str( local_ip) + ':' + str(local_port) proxy = Proxy(uri) return proxy
def raw_socket(uri): blobsize = 40 * 1024 * 1024 num_blobs = 10 total_size = 0 name = threading.currentThread().name with Proxy(uri) as p: print("thread {0} preparing {1} blobs of size {2} Mb".format( name, num_blobs, blobsize / 1024.0 / 1024.0)) blobs = {} for _ in range(num_blobs): file_id, blob_address = p.prepare_file_blob(blobsize) blobs[file_id] = blob_address start = time.time() for file_id in blobs: print( "thread {0} retrieving blob using raw socket...".format(name)) blob_address = blobs[file_id] sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(tuple(blob_address)) sock.sendall(file_id.encode()) size = 0 chunk = "dummy" while chunk: chunk = sock.recv(60000) size += len(chunk) sock.close() assert size == blobsize total_size += size duration = time.time() - start assert total_size == blobsize * num_blobs print("thread {0} done, {1:.2f} Mb/sec.".format( name, total_size / 1024.0 / 1024.0 / duration))
def main(args): if len(args) != 3: print("usage: client.py <robotname> <robottype>") print(" type is one of: %s" % list(observers.keys())) return name = args[1] observertype = args[2] with Daemon() as daemon: observer = observers[observertype]() daemon.register(observer) gameserver = Proxy("PYRONAME:example.robotserver") robot = gameserver.register(name, observer) with robot: # make sure it disconnects, before the daemon thread uses it later robot.emote("Hi there! I'm here to kick your ass") observer.robot = robot print("Pyro server registered on %s" % daemon.locationStr) daemon.requestLoop()
def main(): print("\nThis program will calculate Prime Factorials of a bunch of random numbers.") print("The more workers you will start (on different cpus/cores/machines),") print("the faster you will get the complete list of results!\n") with Proxy("PYRONAME:example.distributed.dispatcher") as dispatcher: placework(dispatcher) numbers = collectresults(dispatcher) printresults(numbers)
def wordfreq(book, counter_uri): begin = time.time() with Proxy(counter_uri) as counter: totals = counter.count(book) totals = Counter(totals) time_taken = round(time.time() - begin, 2) print("Top five words:") for word, counts in totals.most_common(5): print(" %s (%d)" % (word, counts)) print("Time taken:", time_taken, "sec.")
def __init__(self, name, role, children, address, daemon=None, **kwargs): """ Parameters ---------- address: str server address and port of the Microscope server, e.g. "PYRO:Microscope@localhost:4242" timeout: float Time in seconds the client should wait for a response from the server. """ model.HwComponent.__init__(self, name, role, daemon=daemon, **kwargs) self._proxy_access = threading.Lock() try: self.server = Proxy(address) self.server._pyroTimeout = 30 # seconds self._swVersion = self.server.get_software_version() self._hwVersion = self.server.get_hardware_version() except Exception as err: raise HwError( "Failed to connect to XT server '%s'. Check that the " "uri is correct and XT server is" " connected to the network. %s" % (address, err)) # create the scanner child try: kwargs = children["scanner"] except (KeyError, TypeError): raise KeyError("SEM was not given a 'scanner' child") self._scanner = Scanner(parent=self, daemon=daemon, **kwargs) self.children.value.add(self._scanner) # create the stage child, if requested if "stage" in children: ckwargs = children["stage"] self._stage = Stage(parent=self, daemon=daemon, **ckwargs) self.children.value.add(self._stage) # create a focuser, if requested if "focus" in children: ckwargs = children["focus"] self._focus = Focus(parent=self, daemon=daemon, **ckwargs) self.children.value.add(self._focus)
class NamingTrasher(threading.Thread): def __init__(self, nsuri, number): threading.Thread.__init__(self) self.daemon = True self.number = number self.ns = Proxy(nsuri) self.mustStop = False def list(self): items = self.ns.list() def register(self): for i in range(4): try: self.ns.register(randomname(), 'PYRO:objname@host:555') except Pyro5.errors.NamingError: pass def remove(self): self.ns.remove(randomname()) def lookup(self): try: uri = self.ns.lookup(randomname()) except Pyro5.errors.NamingError: pass def listprefix(self): entries = self.ns.list(prefix="stresstest.51") def listregex(self): entries = self.ns.list(regex=r"stresstest\.??\.41.*") def run(self): print("Name Server trasher running.") self.ns._pyroClaimOwnership() while not self.mustStop: random.choice((self.list, self.register, self.remove, self.lookup, self.listregex, self.listprefix))() sys.stdout.write("%d " % self.number) sys.stdout.flush() time.sleep(0.001) print("Trasher exiting.")
def testSerializePyroTypes(self): uri = URI("PYRO:obj@host:9999") ser = self.serializer.dumps(uri) uri2 = self.serializer.loads(ser) assert isinstance(uri2, URI) assert uri2 == uri proxy = Proxy("PYRO:obj@host:9999") proxy._pyroHandshake = "handshake" ser = self.serializer.dumps(proxy) proxy2 = self.serializer.loads(ser) assert isinstance(proxy2, Proxy) assert proxy2 == proxy assert proxy2._pyroHandshake == "handshake" with Daemon(host="localhost", port=12345, nathost="localhost", natport=9876) as daemon: ser = self.serializer.dumps(daemon) daemon2 = self.serializer.loads(ser) assert isinstance(daemon2, Daemon)
def get_maestral_proxy(config_name='maestral', fallback=False): """ Returns a Pyro proxy of the a running Maestral instance. If ``fallback`` is ``True``, a new instance of Maestral will be returned when the daemon cannot be reached. :param str config_name: The name of the Maestral configuration to use. :param bool fallback: If ``True``, a new instance of Maestral will be returned when the daemon cannot be reached. Defaults to ``False``. :returns: Pyro proxy of Maestral or a new instance. :raises: ``Pyro5.errors.CommunicationError`` if the daemon cannot be reached and ``fallback`` is ``False``. """ pid = get_maestral_pid(config_name) if pid: sock_name = sockpath_for_config(config_name) sys.excepthook = Pyro5.errors.excepthook maestral_daemon = Proxy(URI.format(config_name, './u:' + sock_name)) try: maestral_daemon._pyroBind() return maestral_daemon except Pyro5.errors.CommunicationError: maestral_daemon._pyroRelease() if fallback: from maestral.main import Maestral m = Maestral(config_name, run=False) m.log_handler_stream.setLevel(logging.CRITICAL) return m else: raise Pyro5.errors.CommunicationError
def wait_for_startup(config_name: str, timeout: float = 5) -> None: """ Waits until we can communicate with the maestral daemon for ``config_name``. :param config_name: Configuration to connect to. :param timeout: Timeout it seconds until we raise an error. :raises CommunicationError: if we cannot communicate with the daemon within the given timeout. """ sock_name = sockpath_for_config(config_name) maestral_daemon = Proxy(URI.format(config_name, "./u:" + sock_name)) t0 = time.time() while True: try: maestral_daemon._pyroBind() return except Exception as exc: if time.time() - t0 > timeout: raise exc else: time.sleep(0.2) finally: maestral_daemon._pyroRelease()
def find_stockmarkets(): # You can hardcode the stockmarket names for nasdaq and newyork, but it # is more flexible if we just look for every available stockmarket. markets = [] with locate_ns() as ns: for market, market_uri in ns.list( prefix="example.stockmarket.").items(): print("found market", market) markets.append(Proxy(market_uri)) if not markets: raise ValueError( "no markets found! (have you started the stock markets first?)") return markets
def test(): with Proxy(uri) as serv: print("\nFIRST: no timezone") try: date1 = serv.echo(datetime.datetime.now()) print("Got from server:", date1) print("{0}\n {1} ({2})".format(date1, repr(date1), type(date1))) if isinstance(date1, datetime.datetime): if hasattr(date1, "tzinfo"): print(" tzinfo =", date1.tzinfo) else: print(" no tzinfo attribute") except: print("ERROR!") traceback.print_exc() print("\nSECOND: PyTz timezones") try: date1 = serv.pytz() print("Got from server:", date1) if isinstance(date1, datetime.datetime): assert isinstance(date1.tzinfo, datetime.tzinfo) print("{0}\n {1} ({2})\n {3}".format( date1, date1.tzinfo, type(date1.tzinfo), date1.strftime(fmt))) date2 = serv.echo(date1) print("{0}\n {1} ({2})\n {3}".format( date2, date2.tzinfo, type(date2.tzinfo), date2.strftime(fmt))) assert date1 == date2 except: print("ERROR!") traceback.print_exc() print("\nTHIRD: DateUtil timezones") try: date1 = serv.dateutil() print("Got from server:", date1) if isinstance(date1, datetime.datetime): assert isinstance(date1.tzinfo, datetime.tzinfo) print("{0}\n {1} ({2})\n {3}".format( date1, date1.tzinfo, type(date1.tzinfo), date1.strftime(fmt))) date2 = serv.echo(date1) print("{0}\n {1} ({2})\n {3}".format( date2, date2.tzinfo, type(date2.tzinfo), date2.strftime(fmt))) assert date1 == date2 except: print("ERROR!") traceback.print_exc()
def testSerializeDumpsAndDumpsCall(self): self.serializer.dumps(uuid.uuid4()) self.serializer.dumps(URI("PYRO:test@test:4444")) self.serializer.dumps(Proxy("PYRONAME:foobar")) self.serializer.dumpsCall("obj", "method", (1, 2, 3), {"arg1": 999}) self.serializer.dumpsCall("obj", "method", (1, 2, array.array('i', [1, 2, 3])), {"arg1": 999}) self.serializer.dumpsCall("obj", "method", (1, 2, array.array('i', [1, 2, 3])), {"arg1": array.array('i', [1, 2, 3])}) self.serializer.dumpsCall("obj", "method", (1, 2, URI("PYRO:test@test:4444")), {"arg1": 999}) self.serializer.dumpsCall("obj", "method", (1, 2, URI("PYRO:test@test:4444")), {"arg1": URI("PYRO:test@test:4444")}) self.serializer.dumpsCall("obj", "method", (1, 2, Proxy("PYRONAME:foobar")), {"arg1": 999}) self.serializer.dumpsCall("obj", "method", (1, 2, Proxy("PYRONAME:foobar")), {"arg1": Proxy("PYRONAME:foobar")})
def __init__(self): self.num_lines_lock = threading.Lock() self.num_lines_ready = 0 self.all_lines_ready = threading.Event() self.result = [] with locate_ns() as ns: mandels = ns.yplookup(meta_any={"class:mandelbrot_calc"}) mandels = list(mandels.items()) print("{0} mandelbrot calculation servers found.".format(len(mandels))) if not mandels: raise ValueError( "launch at least one mandelbrot calculation server before starting this" ) time.sleep(2) self.mandels = [Proxy(uri) for _, (uri, meta) in mandels]
def main(): config = configparser.ConfigParser() # does not throw an error, just returns the empty set if the file doesn't exist config.read(CONFIG.BASEPATH+'/config/iGrill_config.ini') loglevel = config.get("Logging", "LogLevel", fallback="Error") logfile = config.get("Logging", "LogFile", fallback="") parser = argparse.ArgumentParser( description='Sets the LEDs on the SSR control board') parser.add_argument( '-l', '--log-level', action='store', dest='log_level', default=loglevel, help='Set log level, default: \'' + loglevel + '\'') parser.add_argument( '-d', '--log-destination', action='store', dest='log_destination', default=logfile, help='Set log destination (file), default: \'' + logfile + '\'') parser.add_argument( '--low_battery', action='store_true', dest='low_battery', help='Turns on the low battery LED') parser.add_argument( '--done', action='store_true', dest='done', help='Turns on the smoking complete LED') parser.add_argument( '--exit', dest='shutdown', help='Tells the daemon to shutdown', action='store_true') options = parser.parse_args() SetupLog(options.log_level, options.log_destination) buzzObj = Proxy(("PYRO:{}@{}:{}").format( BUZZ.DAEMON.PYRO_OBJECT_ID, BUZZ.DAEMON.PYRO_HOST, BUZZ.DAEMON.PYRO_PORT)) if(options.done): buzzObj.Done() elif(options.low_battery): buzzObj.LowBattery() else: buzzObj.Stop() if(options.shutdown): buzzObj.Exit()
def screen(self, start, width): dr = width / self.res_x di = dr * (self.res_x / self.res_y) di *= 0.8 # aspect ratio correction self.result = ["?"] * self.res_y servers = [BatchProxy(Proxy(uri)) for uri in self.mandels] with futures.ThreadPoolExecutor(max_workers=len(servers) * 2) as pool: for i in range(self.res_y): server = servers[i % len(servers)] server.calc_line(start, self.res_x, i * di, dr, i) tasks = [pool.submit(server) for server in servers] for task in futures.as_completed(tasks): lines = task.result() for (linenr, line) in lines: self.result[linenr] = line return "\n".join(self.result)
def do_test_chunks(): with Proxy(uri) as p: totalsize = 0 begin = time.time() for chunk in p.download_chunks(datasize * 10): chunk = serpent.tobytes(chunk) # in case of serpent encoded bytes totalsize += len(chunk) print(".", end="", flush=True) assert totalsize == datasize * 10 duration = time.time() - begin totalsize = float(totalsize) print("\nIt took %.2f seconds to transfer %d mb." % (duration, totalsize / 1024 / 1024)) print("That is %.0f kb/sec. = %.1f mb/sec. (serializer: %s)" % (totalsize / 1024 / duration, totalsize / 1024 / 1024 / duration, config.SERIALIZER))