当前位置: 首页>>代码示例>>Python>>正文


Python greenlet.greenlet方法代码示例

本文整理汇总了Python中greenlet.greenlet方法的典型用法代码示例。如果您正苦于以下问题:Python greenlet.greenlet方法的具体用法?Python greenlet.greenlet怎么用?Python greenlet.greenlet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在greenlet的用法示例。


在下文中一共展示了greenlet.greenlet方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __new__

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def __new__(cls, func, sentinel=""):
        if greenlet is None:
            raise RuntimeError("IterI requires greenlet support")
        stream = object.__new__(cls)
        stream._parent = greenlet.getcurrent()
        stream._buffer = []
        stream.closed = False
        stream.sentinel = sentinel
        stream.pos = 0

        def run():
            func(stream)
            stream.close()

        g = greenlet.greenlet(run, stream._parent)
        while 1:
            rv = g.switch()
            if not rv:
                return
            yield rv[0] 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:22,代码来源:iterio.py

示例2: __new__

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def __new__(cls, func, sentinel=''):
        if greenlet is None:
            raise RuntimeError('IterI requires greenlet support')
        stream = object.__new__(cls)
        stream._parent = greenlet.getcurrent()
        stream._buffer = []
        stream.closed = False
        stream.sentinel = sentinel
        stream.pos = 0

        def run():
            func(stream)
            stream.close()

        g = greenlet.greenlet(run, stream._parent)
        while 1:
            rv = g.switch()
            if not rv:
                return
            yield rv[0] 
开发者ID:jpush,项目名称:jbox,代码行数:22,代码来源:iterio.py

示例3: synclize

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def synclize(func):
    coro = coroutine(func)

    @wraps(func)
    def _sync_call(*args, **kwargs):
        child_gr = greenlet.getcurrent()
        main = child_gr.parent
        assert main, "only run in child greenlet"

        def callback(future):
            if future.exc_info():
                child_gr.throw(*future.exc_info())
            elif future.exception():
                child_gr.throw(future.exception())
            else:
                child_gr.switch(future.result())

        IOLoop.current().add_future(coro(*args, **kwargs), callback)
        return main.switch()

    return _sync_call 
开发者ID:zhu327,项目名称:greentor,代码行数:23,代码来源:green.py

示例4: wait_conn

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def wait_conn(self):
        timer = None
        child_gr = greenlet.getcurrent()
        main = child_gr.parent
        try:
            if self._wait_timeout:
                timer = Timeout(self._wait_timeout)
                timer.start()
            self._wait.append(child_gr.switch)
            return main.switch()
        except TimeoutException:
            raise Exception("timeout wait connections, connections size %s",
                            self.size)
        finally:
            if timer:
                timer.cancel() 
开发者ID:zhu327,项目名称:greentor,代码行数:18,代码来源:green.py

示例5: _startup

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def _startup(rootFuture, *args, **kargs):
    """Initializes the SCOOP environment.

    :param rootFuture: Any callable object (function or class object with *__call__*
        method); this object will be called once and allows the use of parallel
        calls inside this object.
    :param args: A tuple of positional arguments that will be passed to the
        callable object.
    :param kargs: A dictionary of additional keyword arguments that will be
        passed to the callable object.

    :returns: The result of the root Future.

    Be sure to launch your root Future using this method."""
    import greenlet
    global _controller
    _controller = greenlet.greenlet(control.runController)
    try:
        result = _controller.switch(rootFuture, *args, **kargs)
    except scoop._comm.Shutdown:
        result = None
        control.execQueue.shutdown()
    return result 
开发者ID:soravux,项目名称:scoop,代码行数:25,代码来源:futures.py

示例6: test_greenlet

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def test_greenlet(self):
        GREENLET = """\
            from greenlet import greenlet

            def test1(x, y):
                z = gr2.switch(x+y)
                print(z)

            def test2(u):
                print(u)
                gr1.switch(42)

            gr1 = greenlet(test1)
            gr2 = greenlet(test2)
            gr1.switch("hello", " world")
            """
        self.try_some_code(GREENLET, "greenlet", greenlet, "hello world\n42\n") 
开发者ID:nedbat,项目名称:coveragepy-bbmirror,代码行数:19,代码来源:test_concurrency.py

示例7: test_greenlet_context_copying

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def test_greenlet_context_copying(self):
        app = flask.Flask(__name__)
        greenlets = []

        @app.route('/')
        def index():
            reqctx = flask._request_ctx_stack.top.copy()
            def g():
                self.assert_false(flask.request)
                self.assert_false(flask.current_app)
                with reqctx:
                    self.assert_true(flask.request)
                    self.assert_equal(flask.current_app, app)
                    self.assert_equal(flask.request.path, '/')
                    self.assert_equal(flask.request.args['foo'], 'bar')
                self.assert_false(flask.request)
                return 42
            greenlets.append(greenlet(g))
            return 'Hello World!'

        rv = app.test_client().get('/?foo=bar')
        self.assert_equal(rv.data, b'Hello World!')

        result = greenlets[0].run()
        self.assert_equal(result, 42) 
开发者ID:chalasr,项目名称:Flask-P2P,代码行数:27,代码来源:reqctx.py

示例8: test_greenlet_context_copying_api

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def test_greenlet_context_copying_api(self):
        app = flask.Flask(__name__)
        greenlets = []

        @app.route('/')
        def index():
            reqctx = flask._request_ctx_stack.top.copy()
            @flask.copy_current_request_context
            def g():
                self.assert_true(flask.request)
                self.assert_equal(flask.current_app, app)
                self.assert_equal(flask.request.path, '/')
                self.assert_equal(flask.request.args['foo'], 'bar')
                return 42
            greenlets.append(greenlet(g))
            return 'Hello World!'

        rv = app.test_client().get('/?foo=bar')
        self.assert_equal(rv.data, b'Hello World!')

        result = greenlets[0].run()
        self.assert_equal(result, 42)

    # Disable test if we don't have greenlets available 
开发者ID:chalasr,项目名称:Flask-P2P,代码行数:26,代码来源:reqctx.py

示例9: _capture_profile

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def _capture_profile(fname=''):
    if not fname:
        yappi.set_clock_type('cpu')
        # We need to set context to greenlet to profile greenlets
        # https://bitbucket.org/sumerc/yappi/pull-requests/3
        yappi.set_context_id_callback(
            lambda: id(greenlet.getcurrent()))
        yappi.set_context_name_callback(
            lambda: greenlet.getcurrent().__class__.__name__)
        yappi.start()
    else:
        yappi.stop()
        stats = yappi.get_func_stats()
        # User should provide filename. This file with a suffix .prof
        # will be created in temp directory.
        try:
            stats_file = os.path.join(tempfile.gettempdir(), fname + '.prof')
            stats.save(stats_file, "pstat")
        except Exception as e:
            print("Error while saving the trace stats ", str(e))
        finally:
            yappi.clear_stats() 
开发者ID:openstack,项目名称:oslo.service,代码行数:24,代码来源:eventlet_backdoor.py

示例10: async_call_method

# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import greenlet [as 别名]
def async_call_method(fun, *args, **kwargs):
    future = platform.Future()
    ioloop = platform.current_ioloop()

    def finish():
        try:
            result = fun(*args, **kwargs)
            if future._callbacks:
                ioloop.call_soon(future.set_result, result)
            else:
                future.set_result(result)
        except Exception as e:
            if future._callbacks:
                ioloop.call_soon(future.set_exception, e)
            else:
                future.set_exception(e)

    child_gr = greenlet.greenlet(finish)
    child_gr.switch()

    return future 
开发者ID:snower,项目名称:TorMySQL,代码行数:23,代码来源:util.py


注:本文中的greenlet.greenlet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。