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.dc2.wordcount.") # chop the text into chunks that can be distributed across the workers # uses futures so that it runs the counts in parallel # counter is selected in a round-robin fashion from list of all available counters with futures.ThreadPoolExecutor() as pool: roundrobin_counters = cycle(all_counters.values()) tasks = [] for chunk in grouper(200, lines): tasks.append( pool.submit(self.count_chunk, next(roundrobin_counters), chunk)) # gather the results print("Collecting %d results (counted in parallel)..." % len(tasks)) totals = Counter() for task in futures.as_completed(tasks): try: totals.update(task.result()) except Pyro5.errors.CommunicationError as x: raise Pyro5.errors.PyroError( "Something went wrong in the server when collecting the responses: " + str(x)) return totals
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 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): self.result = [] with locate_ns() as ns: mandels = ns.yplookup(meta_any={"class:mandelbrot_calc"}) self.mandels = [uri for _, (uri, meta) in mandels.items()] print("{0} mandelbrot calculation servers found.".format( len(self.mandels))) if not mandels: raise ValueError( "launch at least one mandelbrot calculation server before starting this" ) time.sleep(2)
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 __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 __init__(self): self.root = tkinter.Tk() self.root.title("Mandelbrot (Pyro multi CPU core version)") canvas = tkinter.Canvas(self.root, width=res_x, height=res_y, bg="#000000") canvas.pack() self.img = tkinter.PhotoImage(width=res_x, height=res_y) canvas.create_image((res_x/2, res_y/2), image=self.img, state="normal") with locate_ns() as ns: mandels = ns.yplookup(meta_any={"class:mandelbrot_calc_color"}) 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") self.mandels = [uri for _, (uri, meta) in mandels] self.pool = futures.ThreadPoolExecutor(max_workers=len(self.mandels)) self.tasks = [] self.start_time = time.time() for line in range(res_y): self.tasks.append(self.calc_new_line(line)) self.root.after(100, self.draw_results) tkinter.mainloop()
def __init__(self): self.root = tkinter.Tk() self.root.title("Mandelbrot (Pyro multi CPU core version)") canvas = tkinter.Canvas(self.root, width=res_x, height=res_y, bg="#000000") canvas.pack() self.img = tkinter.PhotoImage(width=res_x, height=res_y) canvas.create_image((res_x/2, res_y/2), image=self.img, state="normal") with locate_ns() as ns: mandels = ns.yplookup(meta_any={"class:mandelbrot_calc_color"}) 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") self.mandels = [Proxy(uri) for _, (uri, meta) in mandels] #for m in self.mandels: # m._pyroAsync() # set them to asynchronous mode # @todo the calls in the client are processed sequentially because Pyro5 no longer has async proxies itself - FIX THIS for proxy in self.mandels: proxy._pyroBind() self.lines = list(reversed(range(res_y))) self.draw_data = Queue() self.root.after(1000, self.draw_lines) tkinter.mainloop()
def main(): threads = [] ns = locate_ns() print("Removing previous stresstest registrations...") ns.remove(prefix="stresstest.") print("Done. Starting.") for i in range(5): nt = NamingTrasher(ns._pyroUri, i) nt.start() threads.append(nt) try: while True: time.sleep(1) except KeyboardInterrupt: pass print("Break-- waiting for threads to stop.") for nt in threads: nt.mustStop = True nt.join() count = ns.remove(prefix="stresstest.") print("cleaned up %d names." % count)
from Pyro5.api import locate_ns, Daemon, type_meta, Proxy from resources import LaserPrinter, MatrixPrinter, PhotoPrinter, TapeStorage, DiskStorage, Telephone, Faxmachine # register various objects with some metadata describing their resource class ns = locate_ns() d = Daemon() uri = d.register(LaserPrinter) ns.register("example.resource.laserprinter", uri, metadata=type_meta(LaserPrinter) | {"resource:printer", "performance:fast"}) uri = d.register(MatrixPrinter) ns.register("example.resource.matrixprinter", uri, metadata=type_meta(MatrixPrinter) | {"resource:printer", "performance:slow"}) uri = d.register(PhotoPrinter) ns.register("example.resource.photoprinter", uri, metadata=type_meta(PhotoPrinter) | {"resource:printer", "performance:slow"}) uri = d.register(TapeStorage) ns.register("example.resource.tapestorage", uri, metadata=type_meta(TapeStorage) | {"resource:storage", "performance:slow"}) uri = d.register(DiskStorage) ns.register("example.resource.diskstorage", uri, metadata=type_meta(DiskStorage)
self._name = marketname self._symbols = symbols def quotes(self): while True: symbol = random.choice(self.symbols) yield symbol, round(random.uniform(5, 150), 2) time.sleep(random.random()/2.0) @property def name(self): return self._name @property def symbols(self): return self._symbols if __name__ == "__main__": nasdaq = StockMarket("NASDAQ", ["AAPL", "CSCO", "MSFT", "GOOG"]) newyork = StockMarket("NYSE", ["IBM", "HPQ", "BP"]) # for example purposes we will access the daemon and name server ourselves and not use serveSimple with Daemon() as daemon: nasdaq_uri = daemon.register(nasdaq) newyork_uri = daemon.register(newyork) with locate_ns() as ns: ns.register("example.stockmarket.nasdaq", nasdaq_uri) ns.register("example.stockmarket.newyork", newyork_uri) print("Stockmarkets available.") daemon.requestLoop()
import Pyro5.api as p5 ns = p5.locate_ns() uri = ns.lookup('obj') #o = Pyro4.Proxy('PYRO:obj_6bc3296b2d8745828247253eb0a654e1@localhost:54218') o = p5.Proxy(uri) print(o.listarPerfil('*****@*****.**'))
def main(log_to_file=True): import pkg_resources version = pkg_resources.require('humidifier-controller')[0].version print('Starting Humidifier Controller Server, version: ' + version) config.SERVERTYPE = 'multiplex' daemon = Daemon() def start_client_logger(): from threading import Thread t = Thread(target=Logger, kwargs={'log_to_file': log_to_file}, daemon=True) t.start() return t def signal_handler(signo, frame): if daemon != None: daemon.transportServer.shutting_down = True print(f'Shutting down gracefully exit code: {signo}') daemon.shutdown() from threading import enumerate logger_threads = [ thread for thread in enumerate() if isinstance(thread, Logger) ] for logger_thread in logger_threads: print("Stopping {}.".format(logger_thread)) logger_thread.stop() sys.exit(1) import signal for sig in ('TERM', 'HUP', 'INT'): signal.signal(getattr(signal, 'SIG' + sig), signal_handler) nameserverDaemon = None try: nameserverUri, nameserverDaemon, broadcastServer = start_ns() assert broadcastServer is not None, "expect a broadcast server to be created" print("got a Nameserver, uri=%s" % nameserverUri) except OSError: print('Pyro nameserver already running. No idea who started it.') if (nameserverDaemon == None): with locate_ns() as ns: try: ns.lookup('serial_server.serial_connection') print("Serial server is already registered. Aborting.") return 0 except: pass serial_connection = SerialConnection(baudrate='9600') serial_connection.set_settings(serial_connection.read_settings()) serial_connection.post_message('\0') serial_uri = daemon.register(serial_connection) if (nameserverDaemon == None): with locate_ns() as ns: ns.register('serial_server.serial_connection', serial_uri) else: nameserverDaemon.nameserver.register('serial_server.serial_connection', serial_uri) daemon.combine(nameserverDaemon) daemon.combine(broadcastServer) print('Serial connection registered.') start_client_logger() daemon.requestLoop()