Example #1
0
    def test_mixing_greenlet_coroutine(self):
        from _stackless import greenlet, coroutine
        lst = []

        def f():
            lst.append(1)
            greenlet.getcurrent().parent.switch()
            lst.append(3)

        def make_h(c):
            def h():
                g = greenlet(f)
                lst.append(0)
                g.switch()
                c.switch()
                lst.append(2)
                g.switch()
                c.switch()
                lst.append(4)
                c.switch()

            return h

        c1 = coroutine.getcurrent()
        c2 = coroutine()
        c3 = coroutine()
        c2.bind(make_h(c3))
        c3.bind(make_h(c2))
        c2.switch()
        assert lst == [0, 1, 0, 1, 2, 3, 2, 3, 4, 4]
Example #2
0
def _init():
    global _main_tasklet
    global _global_task_id
    global _squeue
    global _last_task
    _global_task_id = 0
    _main_tasklet = coroutine.getcurrent()
    try:
        _main_tasklet.__class__ = tasklet
    except TypeError:  # we are running pypy-c

        class TaskletProxy(object):
            """TaskletProxy is needed to give the _main_coroutine tasklet behaviour"""
            def __init__(self, coro):
                self._coro = coro

            def __getattr__(self, attr):
                return getattr(self._coro, attr)

            def __str__(self):
                return '<tasklet %s a:%s>' % (self._task_id, self.is_alive)

            def __reduce__(self):
                return getmain, ()

            __repr__ = __str__

        global _main_coroutine
        _main_coroutine = _main_tasklet
        _main_tasklet = TaskletProxy(_main_tasklet)
        assert _main_tasklet.is_alive and not _main_tasklet.is_zombie
    _last_task = _main_tasklet
    tasklet._init.im_func(_main_tasklet, label='main')
    _squeue = deque()
    _scheduler_append(_main_tasklet)
Example #3
0
def _init():
    global _main_tasklet
    global _global_task_id
    global _squeue
    global _last_task
    _global_task_id = 0
    _main_tasklet = coroutine.getcurrent()
    try:
        _main_tasklet.__class__ = tasklet
    except TypeError: # we are running pypy-c
        class TaskletProxy(object):
            """TaskletProxy is needed to give the _main_coroutine tasklet behaviour"""
            def __init__(self, coro):
                self._coro = coro

            def __getattr__(self,attr):
                return getattr(self._coro,attr)

            def __str__(self):
                return '<tasklet %s a:%s>' % (self._task_id, self.is_alive)

            def __reduce__(self):
                return getmain, ()

            __repr__ = __str__


        global _main_coroutine
        _main_coroutine = _main_tasklet
        _main_tasklet = TaskletProxy(_main_tasklet)
        assert _main_tasklet.is_alive and not _main_tasklet.is_zombie
    _last_task = _main_tasklet
    tasklet._init.im_func(_main_tasklet, label='main')
    _squeue = deque()
    _scheduler_append(_main_tasklet)
Example #4
0
 def test_mixing_greenlet_coroutine(self):
     from _stackless import greenlet, coroutine
     lst = []
     def f():
         lst.append(1)
         greenlet.getcurrent().parent.switch()
         lst.append(3)
     def make_h(c):
         def h():
             g = greenlet(f)
             lst.append(0)
             g.switch()
             c.switch()
             lst.append(2)
             g.switch()
             c.switch()
             lst.append(4)
             c.switch()
         return h
     c1 = coroutine.getcurrent()
     c2 = coroutine()
     c3 = coroutine()
     c2.bind(make_h(c3))
     c3.bind(make_h(c2))
     c2.switch()
     assert lst == [0, 1, 0, 1, 2, 3, 2, 3, 4, 4]
Example #5
0
def getcurrent():
    """
    getcurrent() -- return the currently executing tasklet.
    """

    curr = coroutine.getcurrent()
    if curr is _main_coroutine:
        return _main_tasklet
    else:
        return curr
Example #6
0
def __init():
    global main_tasklet
    global main_coroutine
    global scheduler 
    main_coroutine = c = coroutine.getcurrent()
    main_tasklet = TaskletProxy(c)
    SETNEXT(main_tasklet, main_tasklet)
    SETPREV(main_tasklet, main_tasklet)
    main_tasklet.is_main = True
    scheduler = Scheduler()
Example #7
0
def getcurrent():
    """
    getcurrent() -- return the currently executing tasklet.
    """

    curr = coroutine.getcurrent()
    if curr is main_coroutine:
        return main_tasklet
    else:
        return curr