Exemplo n.º 1
0
 def __init__(self):
     g = api.Greenlet(self.run)
     BaseTwistedHub.__init__(self, g)
     assert Hub.state == 0, ('%s hub can only be instantiated once' %
                             type(self).__name__, Hub.state)
     Hub.state = 1
     make_twisted_threadpool_daemonic(
     )  # otherwise the program would hang after the main greenlet exited
Exemplo n.º 2
0
def spawn_greenlet(function, *args):
    """Create a new greenlet that will run `function(*args)'.
    The current greenlet won't be unscheduled. Keyword arguments aren't
    supported (limitation of greenlet), use spawn() to work around that.
    """
    g = api.Greenlet(function)
    g.parent = api.get_hub().greenlet
    api.get_hub().schedule_call_global(0, g.switch, *args)
    return g
Exemplo n.º 3
0
 def switch(self):
     assert api.getcurrent() is not self.greenlet, "Cannot switch from MAINLOOP to MAINLOOP"
     if self.greenlet.dead:
         self.greenlet = api.Greenlet(self.run)
     try:
         api.getcurrent().parent = self.greenlet
     except ValueError:
         pass
     return self.greenlet.switch()
Exemplo n.º 4
0
 def __init__(self, clock=time.time):
     event.init()
     self.clock = clock
     self.readers = {}
     self.writers = {}
     self.excs = {}
     self.greenlet = api.Greenlet(self.run)
     self.exc_info = None
     self.signal(
         2, lambda signalnum, frame: self.greenlet.parent.throw(
             KeyboardInterrupt))
Exemplo n.º 5
0
 def switch(self):
     cur = api.getcurrent()
     assert cur is not self.greenlet, 'Cannot switch to MAINLOOP from MAINLOOP'
     switch_out = getattr(cur, 'switch_out', None)
     if switch_out is not None:
         try:
             switch_out()
         except:
             traceback.print_exception(*sys.exc_info())
     if self.greenlet.dead:
         self.greenlet = api.Greenlet(self.run)
     try:
         api.getcurrent().parent = self.greenlet
     except ValueError:
         pass
     return self.greenlet.switch()