def _load_tasks(self): if 'plugins' not in self._config: logger.warn('no plugin configured.') return plugins = self._config['plugins'] thread_targets_count = 0 for module_name, functions in plugins.items(): logger.debug('search for tasks in module %s', module_name) module = import_module(module_name) logger.debug('module %s successfully imported', module_name) for task_name in functions: logger.debug('checking declared task %s', task_name) if hasattr(module, task_name): candidate_task = getattr(module, task_name) task_type = '' if asyncio.iscoroutinefunction(candidate_task): self._tasks.append(asyncio.async( candidate_task(self))) task_type = 'coroutine' else: self._tasks.append(self._loop.run_in_executor( None, candidate_task, self)) task_type = 'function' thread_targets_count += 1 logger.info('task found: %s.%s, type=%s', module_name, task_name, task_type) if thread_targets_count > 0: self._loop.set_default_executor(ThreadPoolExecutor( max_workers=thread_targets_count + 2) ) logger.debug('configured tasks: %s', self._tasks)
def _load_tasks(self): if 'plugins' not in self._config: logger.warn('no plugin configured.') return plugins = self._config['plugins'] thread_targets_count = 0 for module_name, functions in plugins.items(): logger.debug('search for tasks in module %s', module_name) module = import_module(module_name) logger.debug('module %s successfully imported', module_name) for task_name in functions: logger.debug('checking declared task %s', task_name) if hasattr(module, task_name): candidate_task = getattr(module, task_name) task_type = '' if asyncio.iscoroutinefunction(candidate_task): self._tasks.append(asyncio. async ( candidate_task(self))) task_type = 'coroutine' else: self._tasks.append( self._loop.run_in_executor(None, candidate_task, self)) task_type = 'function' thread_targets_count += 1 logger.info('task found: %s.%s, type=%s', module_name, task_name, task_type) if thread_targets_count > 0: self._loop.set_default_executor( ThreadPoolExecutor(max_workers=thread_targets_count + 2)) logger.debug('configured tasks: %s', self._tasks)
def run_event(self): """get the asyncio.Event or threading.Event""" cf = inspect.currentframe() caller_name = cf.f_back.f_code.co_name caller = cf.f_back.f_globals[caller_name] if asyncio.iscoroutinefunction(caller) or asyncio.iscoroutine(caller): return self._aio_run_event else: return self._thr_run_event
def call_as_future(callable, loop, *args, **kwargs): """This is a copy of thrift.util.asyncio. So, let's consider unifying them. call_as_future(callable, *args, **kwargs) -> trollius.Task Like trollius.ensure_future() but takes any callable and converts it to a coroutine function first. """ if not asyncio.iscoroutinefunction(callable): callable = asyncio.coroutine(callable) return asyncio.ensure_future(callable(*args, **kwargs), loop=loop)
def call_as_future(callable, *args, **kwargs): """This is a copy of thrift.util.asyncio. So, let's consider unifying them. call_as_future(callable, *args, **kwargs) -> trollius.Task Like trollius.ensure_future() but takes any callable and converts it to a coroutine function first. """ if not asyncio.iscoroutinefunction(callable): callable = asyncio.coroutine(callable) return asyncio.ensure_future(callable(*args, **kwargs))
def write_body(self, file, length=None): '''Send the request's content body. Coroutine. ''' _logger.debug('Sending body.') file_is_async = (trollius.iscoroutine(file.read) or trollius.iscoroutinefunction(file.read)) _logger.debug(__('Body is async: {0}', file_is_async)) if length is not None: bytes_left = length while True: if length is not None: if bytes_left <= 0: break read_size = min(bytes_left, self._read_size) else: read_size = self._read_size if file_is_async: data = yield From(file.read(read_size)) else: data = file.read(read_size) if not data: break self._data_observer.notify('request_body', data) if bytes_left <= self._read_size: # XXX: Connection lost is raised too early on Python 3.2, 3.3 # so don't flush on the last chunk but check for connection # closed on reads drain = False else: drain = True yield From(self._connection.write(data, drain=drain)) if length is not None: bytes_left -= len(data)
def wrapper(*args, **kwargs): f = fn if asyncio.iscoroutinefunction(fn) else asyncio.coroutine(fn) loop.run_until_complete(f(*args, **kwargs))