def fmain(seen): try: greenlet.getcurrent().parent.switch() except: seen.append(sys.exc_info()[0]) raise raise SomeError
def Yield(value): g = greenlet.getcurrent() while not isinstance(g, genlet): if g is None: raise RuntimeError, 'yield outside a genlet' g = g.parent g.parent.switch(value)
def next(self): self.parent = greenlet.getcurrent() result = self.switch() if self: return result else: raise StopIteration
def Yield(value, level=1): g = greenlet.getcurrent() while level != 0: if not isinstance(g, genlet): raise RuntimeError, 'yield outside a genlet' if level > 1: g.parent.set_child(g) g = g.parent level -= 1 g.switch(value)
def Yield(value, level = 1): g = greenlet.getcurrent() while level != 0: if not isinstance(g, genlet): raise RuntimeError, 'yield outside a genlet' if level > 1: g.parent.set_child(g) g = g.parent level -= 1 g.switch(value)
def __new__(cls, func): if greenlet is None: raise RuntimeError('IterI requires greenlets') stream = object.__new__(cls) stream.__init__(greenlet.getcurrent()) g = greenlet(lambda: func(stream), stream._parent) while 1: rv = g.switch() if not rv: return yield rv[0]
def block_on(self, o, timeout=None): g = greenlet.getcurrent() if timeout is None: it = (sys.maxint, g, EV_TIMEOUT) else: it = (time() + timeout, g, EV_TIMEOUT) heappush(self.sleepers, it) self.blockers.append_to(o, it) r = g.parent.switch() if r is EV_TIMEOUT: self.blockers.remove_from(o, it) raise TimeoutError() return r
def __wait_on_io(self, fd, l, t): g = greenlet.getcurrent() l.append_to(fd, g) if t is not None: heappush(self.sleepers, (t, g, EV_TIMEOUT)) r = g.parent.switch() if r is EV_IO_READY: return elif r is EV_TIMEOUT: l.remove_from(fd, g) raise TimeoutError() else: raise AssertionError('I/O-waiting greenlet awoken with %r' % r)
def _greenlet_execute(incoming_frame): try: chain2go2next = weak[0]._execute(incoming_frame) except: # no exception is supposed to get out of _execute() # better report it directly into the main greenlet then, # and hidden to prevent catching main_greenlet.throw(AssertionError( "unexpected exception out of Coroutine._execute()", *sys.exc_info())) assert 0 del weak[0] greenlet.getcurrent().parent = chain2go2next.greenlet return None # as the result of the FrameChain.switch()
def _greenlet_execute(incoming_frame): try: chain2go2next = weak[0]._execute(incoming_frame) except: # no exception is supposed to get out of _execute() # better report it directly into the main greenlet then, # and hidden to prevent catching main_greenlet.throw( AssertionError( "unexpected exception out of Coroutine._execute()", *sys.exc_info())) assert 0 del weak[0] greenlet.getcurrent().parent = chain2go2next.greenlet return None # as the result of the FrameChain.switch()
def __new__(cls, func): if greenlet is None: raise RuntimeError("IterI requires greenlet support") stream = object.__new__(cls) stream.__init__(greenlet.getcurrent()) def run(): func(stream) stream.flush() g = greenlet(run, stream._parent) while 1: rv = g.switch() if not rv: return yield rv[0]
def next(self): if self.child: child = self.child while child.child: tmp = child child = child.child tmp.child = None result = child.switch() else: self.parent = greenlet.getcurrent() result = self.switch() if self: return result else: raise StopIteration
def test_throw_goes_to_original_parent(): main = greenlet.getcurrent() def f1(): try: main.switch("f1 ready to catch") except IndexError: return "caught" else: return "normal exit" def f2(): main.switch("from f2") g1 = greenlet(f1) g2 = greenlet(f2, parent=g1) py.test.raises(IndexError, g2.throw, IndexError) assert g2.dead assert g1.dead g1 = greenlet(f1) g2 = greenlet(f2, parent=g1) res = g1.switch() assert res == "f1 ready to catch" res = g2.throw(IndexError) assert res == "caught" assert g2.dead assert g1.dead g1 = greenlet(f1) g2 = greenlet(f2, parent=g1) res = g1.switch() assert res == "f1 ready to catch" res = g2.switch() assert res == "from f2" res = g2.throw(IndexError) assert res == "caught" assert g2.dead assert g1.dead
def f1(): f = sys._getframe(0) assert f.f_back is None greenlet.getcurrent().parent.switch(f) return "meaning of life"
There are different coroutine concepts existing in parallel, like plain interp-level coroutines and app-level structures like coroutines, greenlets and tasklets. Every concept is associated with its own costate object. This allows for peaceful co-existence of many concepts. The type of a switch is determined by the target's costate. """ from pypy.interpreter.baseobjspace import Wrappable from pypy.rlib.rstack import yield_current_frame_to_caller, resume_point from pypy.rlib.objectmodel import we_are_translated try: from py.magic import greenlet main_greenlet = greenlet.getcurrent() except (ImportError, ValueError): def greenlet(*args, **kwargs): raise NotImplementedError("need either greenlets or a translated version of pypy") class FrameChain(object): """Greenlet-based emulation of the primitive rstack 'frames' of RPython""" def __init__(self, thunk=None): if thunk: self.greenlet = greenlet(thunk) else: self.greenlet = greenlet.getcurrent() def switch(self): last = FrameChain()
def __init__(self, thunk=None): if thunk: self.greenlet = greenlet(thunk) else: self.greenlet = greenlet.getcurrent()
There are different coroutine concepts existing in parallel, like plain interp-level coroutines and app-level structures like coroutines, greenlets and tasklets. Every concept is associated with its own costate object. This allows for peaceful co-existence of many concepts. The type of a switch is determined by the target's costate. """ from pypy.interpreter.baseobjspace import Wrappable from pypy.rlib.rstack import yield_current_frame_to_caller, resume_point from pypy.rlib.objectmodel import we_are_translated try: from py.magic import greenlet main_greenlet = greenlet.getcurrent() except (ImportError, ValueError): def greenlet(*args, **kwargs): raise NotImplementedError( "need either greenlets or a translated version of pypy") class FrameChain(object): """Greenlet-based emulation of the primitive rstack 'frames' of RPython""" def __init__(self, thunk=None): if thunk: self.greenlet = greenlet(thunk) else: self.greenlet = greenlet.getcurrent()
try: from py.magic import greenlet return greenlet except ImportError: pass try: greenlet = __import__('greenlet').greenlet except ImportError: greenlet = __import_py_magic_greenlet() if greenlet is None: raise getcurrent = greenlet.getcurrent GreenletExit = greenlet.GreenletExit MAIN = greenlet.getcurrent() thread = __import__('thread') threadlocal = thread._local _threadlocal = threadlocal() _threadlocal.Hub = None try: _original_fork = os.fork except AttributeError: _original_fork = None __all__.remove('fork') def _switch_helper(function, args, kwargs): # work around the fact that greenlet.switch does not support keyword args return function(*args, **kwargs)
def __init__(self): self._main_task = tasklet(greenlet=greenlet.getcurrent(), alive=True) #all non blocked tast are in this queue #all tasks are only onces in this queue #the current task is the first item in the queue self._runnable = deque([self._main_task])
def __init__(self): self._main_task = tasklet(greenlet = greenlet.getcurrent(), alive = True) #all non blocked tast are in this queue #all tasks are only onces in this queue #the current task is the first item in the queue self._runnable = deque([self._main_task])
def sleep(self, seconds): g = greenlet.getcurrent() heappush(self.sleepers, (time() + seconds, g, EV_TIMEOUT)) r = g.parent.switch() if not r is EV_TIMEOUT: raise AssertionError('sleeping greenlet awoken with %r' % r)
def switch(*args): return greenlet.getcurrent().parent.switch(*args)
'getcurrent', 'GreenletExit', 'spawn_raw', 'sleep', 'kill', 'signal', 'fork', 'shutdown', 'get_hub', 'Hub', 'Waiter' ] try: greenlet = __import__('greenlet').greenlet except ImportError: try: from py.magic import greenlet except ImportError: raise ImportError( 'gevent requires greenlet: http://pypi.python.org/pypi/greenlet/') getcurrent = greenlet.getcurrent GreenletExit = greenlet.GreenletExit MAIN = greenlet.getcurrent() thread = __import__('thread') threadlocal = thread._local _threadlocal = threadlocal() _threadlocal.Hub = None try: _original_fork = os.fork except AttributeError: _original_fork = None __all__.remove('fork') def _switch_helper(function, args, kwargs): # work around the fact that greenlet.switch does not support keyword args return function(*args, **kwargs)
def f(): lst.append(1) greenlet.getcurrent().parent.switch() lst.append(3)