def inner_wrapper(*args, **kwargs): try: ctx = next(arg for arg in args[:2] if isinstance(arg, Context)) except StopIteration: raise RuntimeError('the first positional argument to {}() has to be a Context ' 'instance'.format(callable_name(func))) from None executor = ctx.require_resource(Executor, resource_name) return asyncio_extras.call_in_executor(func, *args, executor=executor, **kwargs)
def inner_wrapper(*args, **kwargs): try: ctx = next(arg for arg in args[:2] if isinstance(arg, Context)) except StopIteration: raise RuntimeError( 'the first positional argument to {}() has to be a Context ' 'instance'.format(callable_name(func))) from None executor = ctx.require_resource(Executor, resource_name) return asyncio_extras.call_in_executor(func, *args, executor=executor, **kwargs)
def call_in_executor(self, func: Callable, *args, executor: Union[Executor, str] = None, **kwargs) -> Awaitable: """ Call the given callable in an executor. :param func: the callable to call :param args: positional arguments to call the callable with :param executor: either an :class:`~concurrent.futures.Executor` instance, the resource name of one or ``None`` to use the event loop's default executor :param kwargs: keyword arguments to call the callable with :return: an awaitable that resolves to the return value of the call """ assert check_argument_types() if isinstance(executor, str): executor = self.require_resource(Executor, executor) return asyncio_extras.call_in_executor(func, *args, executor=executor, **kwargs)