def get_asyncio_rpc_stack(self, model, rpc_actions): model_name = model.__class__.__name__.lower() stack = [] # Override for cells here if model_name != 'cells': stack.append( RPCCall(self.group_name, [], {}) ) if model_name != self.group_name: stack.append( RPCCall(model_name, [], {}) ) for action in rpc_actions: for key, value in action.items(): if isinstance(value, list): stack.append( RPCCall(key, value, {}) ) elif isinstance(value, dict): stack.append( RPCCall(key, [], value) ) else: stack.append( RPCCall(key, [value], {}) ) return RPCStack( uuid4().hex, NAMESPACE, RESULT_EXPIRE_TIME, stack)
def getattr(self, name): stack = [ RPCCall(self.group_name, [], {}), RPCCall('_datasource', [], {}), RPCCall('getattr', [name], {}) ] rpc_stack = RPCStack( uuid4().hex, NAMESPACE, RESULT_EXPIRE_TIME, stack) return self.future_class(self.rpc_file, rpc_stack)
def meta(self): # Use nodes route for now stack = [ RPCCall('nodes', [], {}), RPCCall('_datasource', [], {}), RPCCall('getattr', ['meta'], {}) ] rpc_stack = RPCStack( uuid4().hex, NAMESPACE, RESULT_EXPIRE_TIME, stack) return Future(self, rpc_stack)
def get_timestamps(self): stack = [ RPCCall(self.group_name, [], {}), RPCCall('_datasource', [], {}), RPCCall('get', ['time'], {}), RPCCall('value', [], {}) ] rpc_stack = RPCStack( uuid4().hex, NAMESPACE, RESULT_EXPIRE_TIME, stack) return self.future_class(self.rpc_file, rpc_stack)
def get(self, key, *args, **kwargs): # Use nodes route for now stack = [ RPCCall('nodes', [], {}), RPCCall('_datasource', [], {}), RPCCall('getattr', [key], {}) ] rpc_stack = RPCStack( uuid4().hex, NAMESPACE, RESULT_EXPIRE_TIME, stack) return Future(self.rpc_file, rpc_stack)
async def test_pubsub(): rpc_client = RPCClient(await rpc_commlayer(b'pub', b'sub')) rpc_server = RPCServer(await rpc_commlayer(b'sub', b'pub')) executor = Executor('PUBSUB', None) rpc_server.register(executor) await rpc_server.rpc_commlayer.do_subscribe() rpc_func_call = RPCCall('get_item', [1], {}) rpc_func_stack = RPCSubStack(uuid4().hex, 'PUBSUB', 300, [rpc_func_call]) async def process_subscriber(rpc_func_stack): subscriber = await rpc_client.subscribe_call(rpc_func_stack) async for item in subscriber.enumerate(): if item > 5: await subscriber.close() # Clean-up await rpc_client.queue.put(b'END') await rpc_client.rpc_commlayer.unsubscribe() funcs = [ rpc_server.serve(), rpc_client.serve(), process_subscriber(rpc_func_stack), ] await asyncio.gather(*funcs) await rpc_client.rpc_commlayer.close() await rpc_server.rpc_commlayer.close()
def get_extent_subset(self, subset_name, target_espg_code=''): stack = [ RPCCall('get_extent_subset', [subset_name], {'target_espg_code': target_espg_code}), ] rpc_stack = RPCStack( uuid4().hex, NAMESPACE, RESULT_EXPIRE_TIME, stack) return Future(self, rpc_stack)
def get_model_extent(self, target_epsg_code='', bbox=[]): stack = [ RPCCall('get_model_extent', [], {'target_epsg_code': target_epsg_code, 'bbox': bbox}), ] rpc_stack = RPCStack( uuid4().hex, NAMESPACE, RESULT_EXPIRE_TIME, stack) return Future(self, rpc_stack)
async def _rpc_call(self, func_name, func_args, func_kwargs): rpc_func_call = RPCCall(func_name, func_args, func_kwargs) # Add rpc_func_call to the stack of methods to be executed stack = self.stack + [rpc_func_call] rpc_func_stack = RPCStack(uuid4().hex, self.namespace, 300, stack) return await self.client.rpc_call(rpc_func_stack)
async def subscribe( self, only_netcdf_results=False, max_items_per_second=None): """ :param rate_limit: maximum number of items returned per second. Cannot be higher than 5 :param only_netcdf_results: if True, only include results that are in the NetCDF """ client = await self.rpc_file.client if only_netcdf_results: self.rpc_stack.stack.append( RPCCall('only_netcdf_results', [], {}) ) if max_items_per_second: self.rpc_stack.stack.append( RPCCall('max_items_per_second', [max_items_per_second], {}) ) rpc_substack = RPCSubStack(**self.rpc_stack.__dict__) return await client.subscribe_call(rpc_substack)
def integer(self): """ Instead of providing the multiply function directly it is now available via the 'integer' property. Note that an RPCCall with 'integer' is added to the RPCStack before any functions on the IntegerClient are executed. This way server-side first 'integer' is executed before 'multiply', allowing to stack functions calls like: res = service_client.integer.multiply(100, 100) """ return IntegerClient(self.client, self.namespace, [RPCCall('integer', (), {})])
async def _rpc_call(self, func_name, func_args, func_kwargs): """ Helper function to wrap a Python function call into a RPCCall. A RPCCall wraps a function by a function's: name: str args: List kwargs: Dict A RPCStack can have multiple RPCCall's. The RPCStack is sent to the RPC server. """ rpc_func_call = RPCCall(func_name, func_args, func_kwargs) rpc_func_stack = RPCStack( uuid4().hex, self.namespace, 300, [rpc_func_call]) # Let the client sent the RPCStack to the server. # The server executes the function specified # by the RPCStack and returns the result to the client. # This result is returned by this method. return await self.client.rpc_call(rpc_func_stack)
async def multiply_with_dataclass(self, x: CustomDataModel): assert isinstance(x, CustomDataModel) rpc_func_call = RPCCall('multiply_with_dataclass', [x], {}) rpc_func_stack = RPCStack(uuid4().hex, 'TEST', 300, [rpc_func_call]) return await self.client.rpc_call(rpc_func_stack)
async def _rpc_call(self, func_name, func_args, func_kwargs): rpc_func_call = RPCCall(func_name, func_args, func_kwargs) rpc_func_stack = RPCStack(uuid4().hex, self.namespace, 300, [rpc_func_call]) return await self.client.rpc_call(rpc_func_stack)
async def multiply(self, x, y=100): rpc_func_call = RPCCall('multiply', [x], {'y': y}) rpc_func_stack = RPCStack(uuid4().hex, 'TEST', 300, [rpc_func_call]) return await self.client.rpc_call(rpc_func_stack)
async def get_item(self, key): rpc_func_call = RPCCall('get_item', [key], {}) rpc_func_stack = RPCStack(uuid4().hex, 'TEST', 300, [rpc_func_call]) return await self.client.rpc_call(rpc_func_stack)
async def custom_error(self): rpc_func_call = RPCCall('custom_error', [], {}) rpc_func_stack = RPCStack(uuid4().hex, 'TEST', 300, [rpc_func_call]) return await self.client.rpc_call(rpc_func_stack)
def rpc_method(self, *args, **kwargs): rpc_func_call = RPCCall(func.__name__, args, kwargs) rpc_func_stack = RPCStack(uuid4().hex, self.namespace, 300, [rpc_func_call]) return self.client.rpc_call(rpc_func_stack)
async def data(self): rpc_func_call = RPCCall('data', [], {}) rpc_func_stack = RPCStack(uuid4().hex, 'TEST', 300, [rpc_func_call]) return await self.client.rpc_call(rpc_func_stack)