Beispiel #1
0
def spawn_raw(function, *args, **kwargs):
    if kwargs:
        g = greenlet(_switch_helper, get_hub())
        core.active_event(g.switch, function, args, kwargs)
        return g
    else:
        g = greenlet(function, get_hub())
        core.active_event(g.switch, *args)
        return g
Beispiel #2
0
 def __handle_signals(self):
     def f(pending):
         for signal, handler in pending:
             try:
                 handler(signal)
             except Exception:
                 log.warn('Exception in signal-handler', exc_info=True)
     self.pending_signals, pending_signals = [], self.pending_signals
     greenlet(f).switch(pending_signals)
Beispiel #3
0
def spawn_raw(function, *args, **kwargs):
    hub = get_hub()
    if kwargs:
        g = greenlet(_switch_helper, hub)
        hub.loop.run_callback(g.switch, function, args, kwargs)
    else:
        g = greenlet(function, hub)
        hub.loop.run_callback(g.switch, *args)
    return g
Beispiel #4
0
Datei: hub.py Projekt: HVF/gevent
def spawn_raw(function, *args, **kwargs):
    hub = get_hub()
    if kwargs:
        g = greenlet(_switch_helper, hub)
        hub.loop.run_callback(g.switch, function, args, kwargs)
    else:
        g = greenlet(function, hub)
        hub.loop.run_callback(g.switch, *args)
    return g
Beispiel #5
0
def spawn_raw(function, *args, **kwargs):
    if kwargs:
        g = greenlet(_switch_helper, get_hub())
        core.active_event(g.switch, function, args, kwargs)
        return g
    else:
        g = greenlet(function, get_hub())
        core.active_event(g.switch, *args)
        return g
Beispiel #6
0
 def f():
     g1 = greenlet(fmain)
     g1.switch(seen)
     someref.append(g1)
     del g1
     gc.collect()
     lock.release()
     lock2.acquire()
     greenlet()   # trigger release
     lock.release()
     lock2.acquire()
Beispiel #7
0
def test_exception():
    seen = []
    g1 = greenlet(fmain)
    g2 = greenlet(fmain)
    g1.switch(seen)
    g2.switch(seen)
    g2.parent = g1
    assert seen == []
    raises(SomeError, g2.switch)
    assert seen == [SomeError]
    g2.switch()
    assert seen == [SomeError]
Beispiel #8
0
def test_dealloc():
    seen = []
    g1 = greenlet(fmain)
    g2 = greenlet(fmain)
    g1.switch(seen)
    g2.switch(seen)
    assert seen == []
    del g1
    gc.collect()
    assert seen == [greenlet.GreenletExit]
    del g2
    gc.collect()
    assert seen == [greenlet.GreenletExit, greenlet.GreenletExit]
Beispiel #9
0
def send_exception(g, exc):
    # note: send_exception(g, exc)  can be now done with  g.throw(exc).
    # the purpose of this test is to explicitely check the propagation rules.
    def crasher(exc):
        raise exc
    g1 = greenlet(crasher, parent=g)
    g1.switch(exc)
Beispiel #10
0
    def __call__(self, *args, **kwargs):
        """this is where the new task starts to run, e.g. it is where the greenlet is created
        and the 'task' is first scheduled to run"""
        if self.func is None:
            raise TypeError('tasklet function must be a callable')

        def _func(*_args, **_kwargs):
            try:
                self.func(*args, **kwargs)
            except TaskletExit:
                pass #let it pass silently
            except:
                import logging
                logging.exception('unhandled exception in greenlet')
                #don't propagate to parent
            finally:
                assert _scheduler.current == self
                _scheduler.remove(self)
                if _scheduler._runnable: #there are more tasklets scheduled to run next
                    #this make sure that flow will continue in the correct greenlet, e.g. the next in the schedule
                    self.greenlet.parent = _scheduler._runnable[0].greenlet
                self.alive = False            
                del self.greenlet
                del self.func
                del self.data

        self.greenlet = greenlet(_func)
        self.alive = True
        _scheduler.append(self)
        return self
Beispiel #11
0
    def __call__(self, *args, **kwargs):
        """this is where the new task starts to run, e.g. it is where the greenlet is created
        and the 'task' is first scheduled to run"""
        if self.func is None:
            raise TypeError('tasklet function must be a callable')

        def _func(*_args, **_kwargs):
            try:
                self.func(*args, **kwargs)
            except TaskletExit:
                pass  #let it pass silently
            except:
                import logging
                logging.exception('unhandled exception in greenlet')
                #don't propagate to parent
            finally:
                assert _scheduler.current == self
                _scheduler.remove(self)
                if _scheduler._runnable:  #there are more tasklets scheduled to run next
                    #this make sure that flow will continue in the correct greenlet, e.g. the next in the schedule
                    self.greenlet.parent = _scheduler._runnable[0].greenlet
                self.alive = False
                del self.greenlet
                del self.func
                del self.data

        self.greenlet = greenlet(_func)
        self.alive = True
        _scheduler.append(self)
        return self
Beispiel #12
0
    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]
Beispiel #13
0
def test_kill():
    def f():
        switch("ok")
        switch("fail")

    g = greenlet(f)
    res = g.switch()
    assert res == "ok"
    res = g.throw()
    assert isinstance(res, greenlet.GreenletExit)
    assert g.dead
    res = g.throw()    # immediately eaten by the already-dead greenlet
    assert isinstance(res, greenlet.GreenletExit)
Beispiel #14
0
def test_simple():
    lst = []
    def f():
        lst.append(1)
        greenlet.getcurrent().parent.switch()
        lst.append(3)
    g = greenlet(f)
    lst.append(0)
    g.switch()
    lst.append(2)
    g.switch()
    lst.append(4)
    assert lst == range(5)
Beispiel #15
0
def test_frame():
    def f1():
        f = sys._getframe(0)
	assert f.f_back is None
	greenlet.getcurrent().parent.switch(f)
	return "meaning of life"
    g = greenlet(f1)
    frame = g.switch()
    assert frame is g.gr_frame
    assert g
    next = g.switch()
    assert not g
    assert next == "meaning of life"
    assert g.gr_frame is None
Beispiel #16
0
def test_class():
    def f():
        try:
            switch("ok")
        except RuntimeError:
            switch("ok")
            return
        switch("fail")

    g = greenlet(f)
    res = g.switch()
    assert res == "ok"
    res = g.throw(RuntimeError)
    assert res == "ok"
Beispiel #17
0
    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]
Beispiel #18
0
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
Beispiel #19
0
 def __bootstrap(self, f):
     g = greenlet(f)
     g.switch(self)
Beispiel #20
0
 def __init__(self, thunk=None):
     if thunk:
         self.greenlet = greenlet(thunk)
     else:
         self.greenlet = greenlet.getcurrent()
Beispiel #21
0
 def spawn(self, function, args=(), kwargs={}):
     g = greenlet(function)
     heappush(self.sleepers, (0.0, g.parent, EV_TIMEOUT))
     g.parent = g.parent.parent
     g.switch(*args, **kwargs)
Beispiel #22
0
    res = g.switch()
    assert res == "ok"
    res = g.throw(RuntimeError)
    assert res == "ok"

def test_val():
    def f():
        try:
            switch("ok")
        except RuntimeError, val:
            if str(val) == "ciao":
                switch("ok")
                return
        switch("fail")

    g = greenlet(f)
    res = g.switch()
    assert res == "ok"
    res = g.throw(RuntimeError("ciao"))
    assert res == "ok"

    g = greenlet(f)
    res = g.switch()
    assert res == "ok"
    res = g.throw(RuntimeError, "ciao")
    assert res == "ok"

def test_kill():
    def f():
        switch("ok")
        switch("fail")
Beispiel #23
0
from py.magic import greenlet

import greennet
from greennet.queue import Queue


def popper(queue):
    print queue.pop()


if __name__ == '__main__':
    queue = Queue()
    greennet.schedule(greenlet(popper), queue)
    queue.append('hello world')
    greennet.run()

Beispiel #24
0
def test_send_exception():
    seen = []
    g1 = greenlet(fmain)
    g1.switch(seen)
    raises(KeyError, "send_exception(g1, KeyError)")
    assert seen == [KeyError]
Beispiel #25
0
from __future__ import with_statement
from contextlib import closing
import socket

from py.magic import greenlet

import greennet


def echo(sock):
    with closing(sock):
        bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
        while True:
            data = greennet.recv(sock, bufsize)
            if not data:
                break
            greennet.sendall(sock, data)


if __name__ == '__main__':
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', 1234))
    sock.listen(socket.SOMAXCONN)
    with closing(sock):
        while True:
            client, addr = greennet.accept(sock)
            greennet.schedule(greenlet(echo), client)

Beispiel #26
0
 def runner(x):
     g = greenlet(lambda: time.sleep(x))
     g.switch()
Beispiel #27
0
 def __init__(self, thunk=None):
     if thunk:
         self.greenlet = greenlet(thunk)
     else:
         self.greenlet = greenlet.getcurrent()