python类iscoroutine()的实例源码

loop.py 文件源码 项目:uvio 作者: srossross 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def next_tick(self, callback, *args, **kwargs):
        """
        Once the current event loop turn runs to completion,
        call the callback or coroutine function::

            loop.next_tick(callback)


        """
        if inspect.iscoroutinefunction(callback):
            raise Exception("Did you mean ot create a coroutine (got: {})".format(callback))

        if not inspect.iscoroutine(callback):
            callback = partial(callback, *args, **kwargs)

        self.ready[callback] = None
loop.py 文件源码 项目:uvio 作者: srossross 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
def tick(self):

        self.handle_exceptions()

        if not self.ready:
            # Don't let idle block the loop from exiting
            # There should be other handdles blocking exiting if
            # there is nothing ready
            self.unref_ticker()
            return
        coroutine_or_func, future = self.ready.popitem()

        if inspect.iscoroutine(coroutine_or_func):
            process_until_await(self, future, coroutine_or_func)
        else:
            coroutine_or_func()
spider.py 文件源码 项目:wsp 作者: wangybnet 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def crawl(cls, spiders, response, *, middleware=None):
        try:
            if middleware:
                await cls._handle_input(response, middleware)
            res = []
            for spider in spiders:
                for r in spider.parse(response):
                    if inspect.iscoroutine(r):
                        r = await r
                    if r:
                        res.append(r)
            if middleware:
                res = await cls._handle_output(response, res, middleware)
        except Exception as e:
            try:
                if middleware:
                    await cls._handle_error(response, e, middleware)
            except Exception:
                pass
            return ()
        else:
            return res
spider.py 文件源码 项目:wsp 作者: wangybnet 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def start_requests(cls, spiders, start_urls, *, middleware=None):
        try:
            res = []
            for spider in spiders:
                for r in spider.start_requests(start_urls):
                    if inspect.iscoroutine(r):
                        r = await r
                    if r:
                        res.append(r)
            if middleware:
                res = await cls._handle_start_requests(res, middleware)
        except Exception:
            log.warning("Unexpected error occurred when generating start requests", exc_info=True)
            return ()
        else:
            return res
spider.py 文件源码 项目:wsp 作者: wangybnet 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def crawl(cls, spiders, response, *, middleware=None):
        try:
            if middleware:
                await cls._handle_input(response, middleware)
            res = []
            for spider in spiders:
                for r in spider.parse(response):
                    if inspect.iscoroutine(r):
                        r = await r
                    if r:
                        res.append(r)
            if middleware:
                res = await cls._handle_output(response, res, middleware)
        except Exception as e:
            try:
                if middleware:
                    await cls._handle_error(response, e, middleware)
            except Exception:
                pass
            return ()
        else:
            return res
spider.py 文件源码 项目:wsp 作者: wangybnet 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def start_requests(cls, spiders, start_urls, *, middleware=None):
        try:
            res = []
            for spider in spiders:
                for r in spider.start_requests(start_urls):
                    if inspect.iscoroutine(r):
                        r = await r
                    if r:
                        res.append(r)
            if middleware:
                res = await cls._handle_start_requests(res, middleware)
        except Exception:
            log.warning("Unexpected error occurred when generating start requests", exc_info=True)
            return ()
        else:
            return res
discord_transport.py 文件源码 项目:plumeria 作者: sk89q 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def __getattr__(self, item):
        attr = getattr(self.delegate, item)

        if inspect.iscoroutinefunction(attr) or hasattr(attr,
                                                        "_is_coroutine") and attr._is_coroutine or inspect.iscoroutine(
            attr):
            async def wrapper(*args, **kwargs):
                return self._wrap(await attr(*args, **kwargs))

            return wrapper() if inspect.iscoroutine(attr) else wrapper
        elif inspect.isgeneratorfunction(attr) or inspect.isgenerator(attr):
            def wrapper(*args, **kwargs):
                for entry in attr(*args, **kwargs):
                    yield self._wrap(entry)

            return wrapper if inspect.isgeneratorfunction(attr) else wrapper()
        elif inspect.isfunction(attr):
            def wrapper(*args, **kwargs):
                return self._wrap(attr(*args, **kwargs))

            return wrapper
        else:
            return self._wrap(attr)
task.py 文件源码 项目:yaz 作者: yaz 项目源码 文件源码 阅读 67 收藏 0 点赞 0 评论 0
def __call__(self, **kwargs):
        """Prepare dependencies and call this Task"""
        start = datetime.datetime.now()
        logger.info("Start task %s", self)
        try:
            if self.plugin_class:
                result = self.func(get_plugin_instance(self.plugin_class), **kwargs)

            else:
                result = self.func(**kwargs)

            if inspect.iscoroutinefunction(self.func):
                assert inspect.iscoroutine(
                    result), "The task is defined as a coroutine function but does not return a coroutine"
                loop = asyncio.get_event_loop()
                if loop.is_closed():
                    loop = asyncio.new_event_loop()
                    asyncio.set_event_loop(loop)
                result = loop.run_until_complete(result)
                loop.close()
        finally:
            stop = datetime.datetime.now()
            logger.info("End task %s after %s", self, stop - start)

        return result
threads.py 文件源码 项目:asyncio_extras 作者: agronholm 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __await__(self):
        def exec_when_ready():
            event.wait()
            coro.send(None)

            if not self.exited:
                raise RuntimeError('attempted to "await" in a worker thread')

        if self.exited:
            # This is run in the worker thread
            yield
        else:
            # This is run in the event loop thread
            previous_frame = inspect.currentframe().f_back
            coro = next(obj for obj in gc.get_referrers(previous_frame.f_code)
                        if inspect.iscoroutine(obj) and obj.cr_frame is previous_frame)
            event = Event()
            loop = get_event_loop()
            future = loop.run_in_executor(self.executor, exec_when_ready)
            next(future.__await__())  # Make the future think it's being awaited on
            loop.call_soon(event.set)
            yield future
messaging.py 文件源码 项目:CEX.IO-Client-Python3.5 作者: cexioltd 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def is_awaitable(obj):
        # There is no single method which can answer in any case, should wait or not - so need to create one
        # for the suspected cases : func, coro, gen-coro, future,
        #                           class with sync __call__, class with async __call__,
        #                           sync method, async method
        if inspect.isawaitable(obj) or inspect.iscoroutinefunction(obj) or inspect.iscoroutine(obj):
            return True
        elif inspect.isgeneratorfunction(obj):
            return True
        elif CallChain.is_user_defined_class(obj):
            if hasattr(obj, '__call__'):
                return CallChain.is_awaitable(obj.__call__)
            return False
        else:
            return False
testing.py 文件源码 项目:noc-orchestrator 作者: DirceuSilvaLabs 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
testing.py 文件源码 项目:noc-orchestrator 作者: DirceuSilvaLabs 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
testing.py 文件源码 项目:noc-orchestrator 作者: DirceuSilvaLabs 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
testing.py 文件源码 项目:My-Web-Server-Framework-With-Python2.7 作者: syjsu 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
server.py 文件源码 项目:rice 作者: randy3k 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def _is_coroutine(func):
    if sys.version_info > (3, 5, 0):
        return inspect.iscoroutine(func)
    return False
coroutines.py 文件源码 项目:golightan 作者: shirou 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __init__(self, gen, func=None):
        assert inspect.isgenerator(gen) or inspect.iscoroutine(gen), gen
        self.gen = gen
        self.func = func  # Used to unwrap @coroutine decorator
        self._source_traceback = traceback.extract_stack(sys._getframe(1))
        self.__name__ = getattr(gen, '__name__', None)
        self.__qualname__ = getattr(gen, '__qualname__', None)
coroutines.py 文件源码 项目:golightan 作者: shirou 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def iscoroutine(obj):
    """Return True if obj is a coroutine object."""
    return isinstance(obj, _COROUTINE_TYPES)
coroutines.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def __init__(self, gen, func=None):
        assert inspect.isgenerator(gen) or inspect.iscoroutine(gen), gen
        self.gen = gen
        self.func = func  # Used to unwrap @coroutine decorator
        self._source_traceback = traceback.extract_stack(sys._getframe(1))
        self.__name__ = getattr(gen, '__name__', None)
        self.__qualname__ = getattr(gen, '__qualname__', None)
coroutines.py 文件源码 项目:ouroboros 作者: pybee 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def iscoroutine(obj):
    """Return True if obj is a coroutine object."""
    return isinstance(obj, _COROUTINE_TYPES)
testing.py 文件源码 项目:teleport 作者: eomsoft 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
testing.py 文件源码 项目:projects-2017-2 作者: ncss 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
testing.py 文件源码 项目:aweasome_learning 作者: Knight-ZXW 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
thread.py 文件源码 项目:zenchmarks 作者: squeaky-pl 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def AWAIT(coro):
    '''
    Await for a coroutine in an asynchronous thread.  If coro is
    not a proper coroutine, this function acts a no-op, returning coro.
    '''
    if not iscoroutine(coro):
        return coro

    if hasattr(_locals, 'thread'):
        return _locals.thread.AWAIT(coro)
    else:
        raise errors.AsyncOnlyError('Must be used as async')
testing.py 文件源码 项目:zenchmarks 作者: squeaky-pl 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
testing.py 文件源码 项目:browser_vuln_check 作者: lcatro 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
testing.py 文件源码 项目:TornadoWeb 作者: VxCoder 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
testing.py 文件源码 项目:PyQYT 作者: collinsctk 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
maybe.py 文件源码 项目:amino 作者: tek 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def contains_coro(self) -> 'boolean.Boolean':
        return _coconut_tail_call(self.exists, inspect.iscoroutine)
testing.py 文件源码 项目:ProgrameFacil 作者: Gpzim98 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)
testing.py 文件源码 项目:ProgrameFacil 作者: Gpzim98 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType) or iscoroutine(result):
            raise TypeError("Generator and coroutine test methods should be"
                            " decorated with tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)


问题


面经


文章

微信
公众号

扫码关注公众号