How to use the greenlet.greenlet.switch function in greenlet

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 wwqgtxx / wwqLyParse / wwqLyParse / lib / python-3.7.2-embed-amd64 / gevent / _greenlet_primitives.py View on Github external
from weakref import ref as wref
from gc import get_objects

from greenlet import greenlet

from gevent.exceptions import BlockingSwitchOutError


# In Cython, we define these as 'cdef inline' functions. The
# compilation unit cannot have a direct assignment to them (import
# is assignment) without generating a 'lvalue is not valid target'
# error.
locals()['getcurrent'] = __import__('greenlet').getcurrent
locals()['greenlet_init'] = lambda: None
locals()['_greenlet_switch'] = greenlet.switch

__all__ = [
    'TrackedRawGreenlet',
    'SwitchOutGreenletWithLoop',
]

class TrackedRawGreenlet(greenlet):

    def __init__(self, function, parent):
        greenlet.__init__(self, function, parent)
        # See greenlet.py's Greenlet class. We capture the cheap
        # parts to maintain the tree structure, but we do not capture
        # the stack because that's too expensive for 'spawn_raw'.

        current = getcurrent() # pylint:disable=undefined-variable
        self.spawning_greenlet = wref(current)
github gevent / gevent / gevent / hub.py View on Github external
def switch(self):
        switch_out = getattr(getcurrent(), 'switch_out', None)
        if switch_out is not None:
            switch_out()
        return greenlet.switch(self)
github gdeetotdom / thriftworker / thriftworker / hub.py View on Github external
def switch(self):
        switch_out = getattr(getcurrent(), 'switch_out', None)
        if switch_out is not None:
            switch_out()
        return greenlet.switch(self)
github decentfox / tulipcore / gevent / hub.py View on Github external
def switch(self):
        switch_out = getattr(getcurrent(), 'switch_out', None)
        if switch_out is not None:
            switch_out()
        return greenlet.switch(self)
github kenorb-contrib / BitTorrent / B / BTL / greenlet_yielddefer.py View on Github external
def switch(self, *a):
        g = greenlet.getcurrent()
        if (isinstance(g, GreenletWithDeferred) and
            g.finished and g.parent == self):
            # control will return to the parent anyway, and switching to it
            # causes a memory leak (greenlets don't participate in gc).
            if a:
                return a[0]
            return                
        return greenlet.greenlet.switch(self, *a)
github kenorb-contrib / BitTorrent / BTL / greenlet_yielddefer.py View on Github external
def switch(self, *a):
        g = greenlet.getcurrent()
        if (isinstance(g, GreenletWithDeferred) and
            g.finished and g.parent == self):
            # control will return to the parent anyway, and switching to it
            # causes a memory leak (greenlets don't participate in gc).
            if a:
                return a[0]
            return                
        return greenlet.greenlet.switch(self, *a)