How to use greenlet - 10 common examples

To help you get started, we’ve selected a few greenlet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-greenlet / greenlet / run-tests.py View on Github external
spawn(cmd, search_path=0)


# -- find greenlet but skip the one in "."
if not build:
    oldpath = sys.path[:]
    sys.path.remove(here)

import greenlet

if not build:
    sys.path[:] = oldpath

sys.stdout.write("python %s (%s bit) using greenlet %s from %s\n" %
                 (sys.version.split()[0], bits(), greenlet.__version__, greenlet.__file__))
sys.stdout.flush()


# -- run tests
from tests import test_collector
result = unittest.TextTestRunner(verbosity=verbosity).run(test_collector(build_base))
if result.failures or result.errors:
    sys.exit(1)
github cloudera / hue / desktop / core / ext-py / greenlet-0.4.15 / run-tests.py View on Github external
spawn(cmd, search_path=0)


# -- find greenlet but skip the one in "."
if not build:
    oldpath = sys.path[:]
    sys.path.remove(here)

import greenlet

if not build:
    sys.path[:] = oldpath

sys.stdout.write("python %s (%s bit) using greenlet %s from %s\n" %
                 (sys.version.split()[0], bits(), greenlet.__version__, greenlet.__file__))
sys.stdout.flush()


# -- run tests
from tests import test_collector
result = unittest.TextTestRunner(verbosity=verbosity).run(test_collector(build_base))
if result.failures or result.errors:
    sys.exit(1)
github python-greenlet / greenlet / tests / test_greenlet.py View on Github external
def test_switch_to_another_thread(self):
        data = {}
        error = None
        created_event = threading.Event()
        done_event = threading.Event()

        def foo():
            data['g'] = greenlet(lambda: None)
            created_event.set()
            done_event.wait()
        thread = threading.Thread(target=foo)
        thread.start()
        created_event.wait()
        try:
            data['g'].switch()
        except greenlet.error:
            error = sys.exc_info()[1]
        self.assertTrue(error != None, "greenlet.error was not raised!")
        done_event.set()
        thread.join()
github python-greenlet / greenlet / tests / test_throw.py View on Github external
except IndexError:
                return "caught"
            else:
                return "normal exit"

        def f2():
            main.switch("from f2")

        g1 = greenlet(f1)
        g2 = greenlet(f2, parent=g1)
        self.assertRaises(IndexError, g2.throw, IndexError)
        self.assertTrue(g2.dead)
        self.assertTrue(g1.dead)

        g1 = greenlet(f1)
        g2 = greenlet(f2, parent=g1)
        res = g1.switch()
        self.assertEqual(res, "f1 ready to catch")
        res = g2.throw(IndexError)
        self.assertEqual(res, "caught")
        self.assertTrue(g2.dead)
        self.assertTrue(g1.dead)

        g1 = greenlet(f1)
        g2 = greenlet(f2, parent=g1)
        res = g1.switch()
        self.assertEqual(res, "f1 ready to catch")
        res = g2.switch()
        self.assertEqual(res, "from f2")
        res = g2.throw(IndexError)
        self.assertEqual(res, "caught")
        self.assertTrue(g2.dead)
github ajdavis / GreenletProfiler / test / test_greenlet_profiler.py View on Github external
gr_main.switch()
            b()
            spin(1)

        def b():
            spin(5)
            gr_main.switch()
            c()
        
        def c():
            spin(7)
        
        GreenletProfiler.set_clock_type('cpu')
        GreenletProfiler.start(builtins=True)
        gr_main = greenlet.getcurrent()
        g = greenlet.greenlet(a)
        g.switch()
        spin(2)
        g.switch()
        spin(3)
        g.switch()
        self.assertFalse(g, 'greenlet not complete')
        GreenletProfiler.stop()

        ystats = GreenletProfiler.get_func_stats()

        # Check the stats for spin().
        spin_stat = find_func(ystats, 'spin')
        self.assertEqual(5, spin_stat.ncall)
        self.assertAlmostEqual(18 * self.spin_cost, spin_stat.ttot,
                               places=2, msg="spin()'s total time is wrong")
github python-greenlet / greenlet / tests / test_greenlet.py View on Github external
def f():
            greenlet.getcurrent().test = 42
        def deldict(g):
github python-greenlet / greenlet / tests / test_greenlet.py View on Github external
def fmain(seen):
    try:
        greenlet.getcurrent().parent.switch()
    except:
        seen.append(sys.exc_info()[0])
        raise
    raise SomeError
github python-greenlet / greenlet / tests / test_greenlet.py View on Github external
def switchapply():
                apply(greenlet.getcurrent().parent.switch, args, kwargs)
            g = greenlet(switchapply)
github python-greenlet / greenlet / tests / test_greenlet.py View on Github external
greenlet.getcurrent() # update ts_current
                lock2.release() # release gc
            t = threading.Thread(target=another_thread)
            t.start()
            class gc_callback(object):
                def __del__(self):
                    lock1.release()
                    lock2.acquire()
                    recycled[0] = True
            class garbage(object):
                def __init__(self):
                    self.cycle = self
                    self.callback = gc_callback()
            l = []
            x = range(N*2)
            current = greenlet.getcurrent()
            g = garbage()
            for i in x:
                g = None # lose reference to garbage
                if recycled[0]:
                    # gc callback called prematurely
                    t.join()
                    return False
                last = greenlet()
                if recycled[0]:
                    break # yes! gc called in green_new
                l.append(last) # increase allocation counter
            else:
                # gc callback not called when expected
                gc.collect()
                if recycled[0]:
                    t.join()
github python-greenlet / greenlet / tests / test_greenlet.py View on Github external
def worker():
            # wait for the value
            value = greenlet.getcurrent().parent.switch()
            # delete all references to ourself
            del worker[0]
            initiator.parent = greenlet.getcurrent().parent
            # switch to main with the value, but because
            # ts_current is the last reference to us we
            # return immediately
            try:
                greenlet.getcurrent().parent.switch(value)
            finally:
                seen.append(greenlet.getcurrent())
        def initiator():