async def _run(self) -> None: # The `networking` process creates a process pool executor to offload cpu intensive # tasks. We should revisit that when we move the sync in its own process ensure_global_asyncio_executor() self.run_daemon_task(self.handle_network_id_requests()) self.run_daemon(self.get_p2p_server()) self.run_daemon(self.get_event_server()) await self.cancellation()
async def handle_networking_exit(service: BaseService, plugin_manager: PluginManager, endpoint: TrinityEventBusEndpoint) -> None: async with exit_signal_with_service(service): await plugin_manager.shutdown() endpoint.stop() # Retrieve and shutdown the global executor that was created at startup ensure_global_asyncio_executor().shutdown(wait=True)
def launch_node(args: Namespace, trinity_config: TrinityConfig) -> None: with trinity_config.process_id_file('networking'): # The `networking` process creates a process pool executor to offload cpu intensive # tasks. We should revisit that when we move the sync in its own process ensure_global_asyncio_executor() asyncio.ensure_future(launch_node_coro(args, trinity_config)) loop = asyncio.get_event_loop() loop.run_forever() loop.close()
async def get_result(self, request: BaseRequest[TRequestPayload], normalizer: BaseNormalizer[TResponsePayload, TResult], validate_result: Callable[[TResult], None], payload_validator: Callable[[TResponsePayload], None], tracker: BasePerformanceTracker[ BaseRequest[TRequestPayload], TResult], timeout: float = None) -> TResult: if not self.is_operational: if self.service is None or not self.service.is_cancelled: raise ValidationError( f"Must call `launch_service` before sending request to {self._peer}" ) else: raise PeerConnectionLost( f"Response stream closed before sending request to {self._peer}" ) stream = self._response_stream async for payload in stream.payload_candidates(request, tracker, timeout=timeout): try: payload_validator(payload) if normalizer.is_normalization_slow: result = await stream._run_in_executor( # We just retrieve the global executor that was created when the # Node launches. The node manages the lifecycle of the executor. ensure_global_asyncio_executor(), normalizer.normalize_result, payload) else: result = normalizer.normalize_result(payload) validate_result(result) except ValidationError as err: self.service.logger.debug( "Response validation failed for pending %s request from peer %s: %s", stream.response_msg_name, self._peer, err, ) continue else: tracker.record_response( stream.last_response_time, request, result, ) stream.complete_request() return result raise ValidationError( "Manager is not pending a response, but no valid response received" )
def launch_node(args: Namespace, trinity_config: TrinityConfig, endpoint: Endpoint) -> None: with trinity_config.process_id_file('networking'): NodeClass = trinity_config.get_app_config(Eth1AppConfig).node_class node = NodeClass(endpoint, trinity_config) # The `networking` process creates a process pool executor to offload cpu intensive # tasks. We should revisit that when we move the sync in its own process ensure_global_asyncio_executor() loop = node.get_event_loop() endpoint.connect_no_wait(loop) # This is a second PluginManager instance governing plugins in a shared process. plugin_manager = setup_plugins(SharedProcessScope(endpoint), get_all_plugins()) plugin_manager.prepare(args, trinity_config) asyncio.ensure_future(handle_networking_exit(node, plugin_manager, endpoint), loop=loop) asyncio.ensure_future(node.run(), loop=loop) loop.run_forever() loop.close()
def launch_node(args: Namespace, trinity_config: TrinityConfig) -> None: with trinity_config.process_id_file('networking'): endpoint = TrinityEventBusEndpoint() NodeClass = trinity_config.get_app_config(Eth1AppConfig).node_class node = NodeClass(endpoint, trinity_config) # The `networking` process creates a process pool executor to offload cpu intensive # tasks. We should revisit that when we move the sync in its own process ensure_global_asyncio_executor() loop = node.get_event_loop() networking_connection_config = ConnectionConfig.from_name( NETWORKING_EVENTBUS_ENDPOINT, trinity_config.ipc_dir) endpoint.start_serving_nowait( networking_connection_config, loop, ) endpoint.auto_connect_new_announced_endpoints() endpoint.connect_to_endpoints_blocking( ConnectionConfig.from_name(MAIN_EVENTBUS_ENDPOINT, trinity_config.ipc_dir), # Plugins that run within the networking process broadcast and receive on the # the same endpoint networking_connection_config, ) endpoint.announce_endpoint() # This is a second PluginManager instance governing plugins in a shared process. plugin_manager = setup_plugins(SharedProcessScope(endpoint), get_all_plugins()) plugin_manager.prepare(args, trinity_config) asyncio.ensure_future(handle_networking_exit(node, plugin_manager, endpoint), loop=loop) asyncio.ensure_future(node.run(), loop=loop) loop.run_forever() loop.close()
async def _cleanup(self) -> None: self.event_bus.request_shutdown("Node finished unexpectedly") ensure_global_asyncio_executor().shutdown(wait=True)