Python gevent.Greenlet() Examples

The following are 20 code examples of gevent.Greenlet(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gevent , or try the search function .
Example #1
Source File: engine.py    From recipes-py with Apache License 2.0 6 votes vote down vote up
def spawn_greenlet(self, func, args, kwargs, greenlet_name):
    """Returns a gevent.Greenlet which has been initialized with the correct
    greenlet-local-storage state.

    Args:
      * greenlet_name (str|None) - If non-None, assign this to the greenlet's
        name.
    """
    self.close_non_parent_step()

    to_run = [pgs._get_setter_on_spawn() for pgs in PerGreentletStateRegistry]

    current_step = self._step_stack[-1]
    def _runner():
      for fn in to_run:
        fn()
      try:
        return func(*args, **kwargs)
      finally:
        self.close_non_parent_step()
    ret = gevent.spawn(_runner)
    if greenlet_name is not None:
      ret.name = greenlet_name
    current_step.greenlets.append(ret)
    return ret 
Example #2
Source File: gc.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, account_id, namespace_id, label_name, semaphore):
        bind_context(self, 'renamehandler', account_id)
        self.account_id = account_id
        self.namespace_id = namespace_id
        self.label_name = label_name
        self.log = log.new(account_id=account_id)
        self.semaphore = semaphore
        gevent.Greenlet.__init__(self) 
Example #3
Source File: api.py    From recipes-py with Apache License 2.0 5 votes vote down vote up
def spawn_immediate(self, func, *args, **kwargs):
    """Returns a Future to the concurrently running `func(*args, **kwargs)`.

    This is like `spawn`, except that it IMMEDIATELY switches to the new
    Greenlet. You may want to use this if you want to e.g. launch a background
    step and then another step which waits for the daemon.

    Kwargs:

      * __name (str) - If provided, will assign this name to the spawned
        greenlet. Useful if this greenlet ends up raising an exception, this
        name will appear in the stderr logging for the engine. See
        `Future.name` for more information.
      * __meta (any) - If provided, will assign this metadata to the returned
        Future. This field is for your exclusive use.
      * Everything else is passed to `func`.

    Returns a Future of `func`'s result.
    """
    name = kwargs.pop('__name', None)
    meta = kwargs.pop('__meta', None)
    chan = self.make_channel()
    def _immediate_runner():
      chan.get()
      return func(*args, **kwargs)
    ret = self.spawn(_immediate_runner, __name=name, __meta=meta)
    chan.put(None)  # Pass execution to _immediate_runner
    return ret 
Example #4
Source File: gc.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, account_id, namespace_id, provider_name, uid_accessor,
                 message_ttl=DEFAULT_MESSAGE_TTL, thread_ttl=DEFAULT_THREAD_TTL):
        bind_context(self, 'deletehandler', account_id)
        self.account_id = account_id
        self.namespace_id = namespace_id
        self.provider_name = provider_name
        self.uids_for_message = uid_accessor
        self.log = log.new(account_id=account_id)
        self.message_ttl = datetime.timedelta(seconds=message_ttl)
        self.thread_ttl = datetime.timedelta(seconds=thread_ttl)
        gevent.Greenlet.__init__(self) 
Example #5
Source File: actions.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, parent_service, task_timeout=60):
        self.parent_service = weakref.ref(parent_service)
        self.task_timeout = task_timeout
        self.log = logger.new(component='syncback-worker')
        gevent.Greenlet.__init__(self) 
Example #6
Source File: actions.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, syncback_id, process_number, total_processes, poll_interval=1,
                 retry_interval=30, num_workers=NUM_PARALLEL_ACCOUNTS,
                 batch_size=10):
        self.process_number = process_number
        self.total_processes = total_processes
        self.poll_interval = poll_interval
        self.retry_interval = retry_interval
        self.batch_size = batch_size
        self.keep_running = True
        self.workers = gevent.pool.Group()
        # Dictionary account_id -> semaphore to serialize action syncback for
        # any particular account.
        # TODO(emfree): We really only need to serialize actions that operate
        # on any given object. But IMAP actions are already effectively
        # serialized by using an IMAP connection pool of size 1, so it doesn't
        # matter too much.
        self.account_semaphores = defaultdict(lambda: BoundedSemaphore(1))
        # This SyncbackService performs syncback for only and all the accounts
        # on shards it is reponsible for; shards are divided up between
        # running SyncbackServices.
        self.log = logger.new(component='syncback')
        syncback_assignments = config.get("SYNCBACK_ASSIGNMENTS", {})
        if syncback_id in syncback_assignments:
            self.keys = [key for key in engine_manager.engines
                         if key in syncback_assignments[syncback_id] and
                         key % total_processes == process_number]
        else:
            self.log.warn("No shards assigned to syncback server",
                          syncback_id=syncback_id)
            self.keys = []

        self.log = logger.new(component='syncback')
        self.num_workers = num_workers
        self.num_idle_workers = 0
        self.worker_did_finish = gevent.event.Event()
        self.worker_did_finish.clear()
        self.task_queue = Queue()
        self.running_action_ids = set()
        gevent.Greenlet.__init__(self) 
Example #7
Source File: deferred_migration.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.event_queue = event_queue.EventQueue(DEFERRED_ACCOUNT_MIGRATION_EVENT_QUEUE)
        self.redis = self.event_queue.redis
        gevent.Greenlet.__init__(self) 
Example #8
Source File: signal.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def _on_child_hook():
    # This is called in the hub greenlet. To let the function
    # do more useful work, like use blocking functions,
    # we run it in a new greenlet; see gevent.hub.signal
    if callable(_child_handler):
        # None is a valid value for the frame argument
        from gevent import Greenlet
        greenlet = Greenlet(_child_handler, _signal.SIGCHLD, None)
        greenlet.switch() 
Example #9
Source File: signal.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def _on_child_hook():
    # This is called in the hub greenlet. To let the function
    # do more useful work, like use blocking functions,
    # we run it in a new greenlet; see gevent.hub.signal
    if callable(_child_handler):
        # None is a valid value for the frame argument
        from gevent import Greenlet
        greenlet = Greenlet(_child_handler, _signal.SIGCHLD, None)
        greenlet.switch() 
Example #10
Source File: test_it.py    From salesforce-streaming-client with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def start_publish_loop(self, publish_channel, publish_message):
        self.publish_channel = publish_channel
        self.publish_message = publish_message
        self.loop_greenlet = gevent.Greenlet(self._publish_loop)
        self.loop_greenlet.start() 
Example #11
Source File: conftest.py    From raiden-services with MIT License 5 votes vote down vote up
def _get_running_greenlets():
    return [
        obj
        for obj in gc.get_objects()
        if isinstance(obj, gevent.Greenlet) and obj and not obj.dead
    ] 
Example #12
Source File: signal.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def _on_child_hook():
    # This is called in the hub greenlet. To let the function
    # do more useful work, like use blocking functions,
    # we run it in a new greenlet; see gevent.hub.signal
    if callable(_child_handler):
        # None is a valid value for the frame argument
        from gevent import Greenlet
        greenlet = Greenlet(_child_handler, _signal.SIGCHLD, None)
        greenlet.switch() 
Example #13
Source File: concurrent.py    From py-bson-rpc with Mozilla Public License 2.0 5 votes vote down vote up
def _spawn_greenlet(fn, *args, **kwargs):
    from gevent import Greenlet
    g = Greenlet(fn, *args, **kwargs)
    g.start()
    return g 
Example #14
Source File: concurrent.py    From py-bson-rpc with Mozilla Public License 2.0 5 votes vote down vote up
def _spawn_greenlet(fn, *args, **kwargs):
    from gevent import Greenlet
    g = Greenlet(fn, *args, **kwargs)
    g.start()
    return g 
Example #15
Source File: ws.py    From janus-cloud with GNU Affero General Public License v3.0 5 votes vote down vote up
def _async_incoming_msg_handler(self, transport_session, message, exception_handler):
        greenlet = Greenlet(
            self._incoming_msg_handler,
            transport_session,
            message,
        )
        greenlet.link_exception(exception_handler)
        self._msg_handler_pool.start(
            greenlet,
            blocking=True
        ) 
Example #16
Source File: broker.py    From AIT-Core with MIT License 5 votes vote down vote up
def __init__(self):
        self.context = zmq.Context()
        self.XSUB_URL = ait.config.get('server.xsub',
                                        ait.SERVER_DEFAULT_XSUB_URL)
        self.XPUB_URL = ait.config.get('server.xpub',
                                        ait.SERVER_DEFAULT_XPUB_URL)

        ## Name of the topic associated with external commands
        self.command_topic = ait.config.get('command.topic',
                                        ait.DEFAULT_CMD_TOPIC)

        gevent.Greenlet.__init__(self) 
Example #17
Source File: client.py    From AIT-Core with MIT License 5 votes vote down vote up
def __init__(self,
                 zmq_context,
                 zmq_proxy_xsub_url=ait.SERVER_DEFAULT_XSUB_URL,
                 zmq_proxy_xpub_url=ait.SERVER_DEFAULT_XPUB_URL,
                 **kwargs):

        super(ZMQInputClient, self).__init__(zmq_context,
                                             zmq_proxy_xsub_url,
                                             zmq_proxy_xpub_url)

        self.context = zmq_context
        self.sub = self.context.socket(zmq.SUB)
        self.sub.connect(zmq_proxy_xpub_url.replace('*', 'localhost'))

        gevent.Greenlet.__init__(self) 
Example #18
Source File: client.py    From AIT-Core with MIT License 5 votes vote down vote up
def __init__(self,
                 zmq_context,
                 zmq_proxy_xsub_url=ait.SERVER_DEFAULT_XSUB_URL,
                 zmq_proxy_xpub_url=ait.SERVER_DEFAULT_XPUB_URL,
                 **kwargs):

        self.context = zmq_context
        # open PUB socket & connect to broker
        self.pub = self.context.socket(zmq.PUB)
        self.pub.connect(zmq_proxy_xsub_url.replace('*', 'localhost'))

        # calls gevent.Greenlet or gs.DatagramServer __init__
        super(ZMQClient, self).__init__(**kwargs) 
Example #19
Source File: signal.py    From satori with Apache License 2.0 5 votes vote down vote up
def _on_child_hook():
    # This is called in the hub greenlet. To let the function
    # do more useful work, like use blocking functions,
    # we run it in a new greenlet; see gevent.hub.signal
    if callable(_child_handler):
        # None is a valid value for the frame argument
        from gevent import Greenlet
        greenlet = Greenlet(_child_handler, _signal.SIGCHLD, None)
        greenlet.switch() 
Example #20
Source File: async.py    From cascade-server with Apache License 2.0 5 votes vote down vote up
def join_routines(routines):
    greenlets = [_ for _ in routines if isinstance(_, gevent.Greenlet)]
    if enabled:
        gevent.joinall(greenlets)