def __init__(self, config: dict, on_shutdown: Callable[[], None]): super().__init__() self.config = config self._on_shutdown = on_shutdown self._running = False LOGGER.info("Starting process %s", os.getpid()) _log_env() # Log our startup environment. server_port = config['server_port'] LOGGER.info("connecting to napari on port %d.", server_port) # We have to register these before creating the SharedMemoryManager. # Note that we don't have to give the types, just the names. # Although to use them we probably want to know the types! for name in NapariRemoteAPI.RESOURCES: SharedMemoryManager.register(name) # Connect to napari's shared memory on the server_port that napari # passed us in our NAPARI_MON_CLIENT configuration. self._manager = SharedMemoryManager( address=('localhost', config['server_port']), authkey=str.encode('napari'), ) self._manager.connect() # Get the shared resources as a convenient named tuple. self._remote = NapariRemoteAPI.from_manager(self._manager) # Start our thread which will poll napari. self.start()
def __init__(self): # RemoteCommands listens to our run_command event. It executes # commands from the clients. self.events = EmitterGroup(source=self, auto_connect=True, run_command=None) # We must register all callbacks before we create our instance of # SharedMemoryManager. The client must do the same thing, but it # only needs to know the names. We allocate the shared memory. SharedMemoryManager.register('napari_data', callable=self._napari_data) SharedMemoryManager.register('napari_messages', callable=self._napari_messages) SharedMemoryManager.register('napari_shutdown', callable=self._napari_shutdown) SharedMemoryManager.register('client_data', callable=self._client_data) SharedMemoryManager.register('client_messages', callable=self._client_messages) # Start our shared memory server. self._manager = SharedMemoryManager(address=('127.0.0.1', SERVER_PORT), authkey=str.encode(AUTH_KEY)) self._manager.start() # Get the shared resources the server created. Clients will access # these same resources. self._remote = NapariRemoteAPI( self._manager.napari_data(), self._manager.napari_messages(), self._manager.napari_shutdown(), self._manager.client_data(), self._manager.client_messages(), )
def __init__(self): # We expose the run_command event so RemoteCommands can hook to it, # so it can execute commands we receive from clients. self.events = EmitterGroup(source=self, auto_connect=True, run_command=None) # Must register all callbacks before we create our instance of # SharedMemoryManager. SharedMemoryManager.register('napari_shutting_down', callable=self._napari_shutting_down) SharedMemoryManager.register('commands', callable=self._commands) SharedMemoryManager.register('client_messages', callable=self._client_messages) SharedMemoryManager.register('data', callable=self._data) # We ask for port 0 which means let the OS choose a port. We send # the chosen port to the client in its NAPARI_MON_CLIENT variable. self._manager = SharedMemoryManager(address=('127.0.0.1', 0), authkey=str.encode('napari')) self._manager.start() # Get the shared resources. self._remote = NapariRemoteAPI( self._manager.napari_shutting_down(), self._manager.commands(), self._manager.client_messages(), self._manager.data(), )
def __init__(self, config: dict, client_name="?"): super().__init__() assert config self.config = config self.client_name = client_name self.running = True self.napari_data = None LOGGER.info("Starting MonitorClient process %s", os.getpid()) _log_env() server_port = config['server_port'] LOGGER.info("Connecting to port %d...", server_port) # Right now we just need to magically know these callback names, # maybe we can come up with a better way. napari_api = ['shutdown_event', 'command_queue', 'data'] for name in napari_api: SharedMemoryManager.register(name) # Connect to napari's shared memory. self._manager = SharedMemoryManager( address=('localhost', config['server_port']), authkey=str.encode('napari'), ) self._manager.connect() # Get the shared resources. self._shared = SharedResources( self._manager.shutdown_event(), self._manager.command_queue(), self._manager.data(), ) # Start our thread so we can poll napari. self.start()