本文整理匯總了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]
示例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]
示例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
示例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()
示例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
示例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")
示例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)
示例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
示例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()
示例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