python类Greenlet()的实例源码

test_gevent.py 文件源码 项目:core-python 作者: yidao620c 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def test_greenlet(self):
        """??????Greenlet????"""
        class MyGreenlet(gevent.Greenlet):
            def __init__(self, message, n):
                super(MyGreenlet, self).__init__()
                self.message = message
                self.n = n

            def _run(self):
                print(self.message)
                gevent.sleep(self.n)

        g1 = MyGreenlet("Hi there111!", 1)
        g1.start()
        g2 = MyGreenlet("Hi there222!", 2)
        g2.start()
        gevent.joinall([g1, g2])

    # def test_shutdown(self):
    #     def run_forever():
    #         _log.info('run_forever start..')
    #         gevent.sleep(1000)
    #     gevent.signal(signal.SIGQUIT, gevent.kill)
    #     thread = gevent.spawn(run_forever)
    #     thread.join()
greenlet.py 文件源码 项目:dd-trace-py 作者: DataDog 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def __init__(self, *args, **kwargs):
        # get the current Context if available
        current_g = gevent.getcurrent()
        ctx = getattr(current_g, CONTEXT_ATTR, None)

        # create the Greenlet as usual
        super(TracedGreenlet, self).__init__(*args, **kwargs)

        # the context is always available made exception of the main greenlet
        if ctx:
            # create a new context that inherits the current active span
            # TODO: a better API for Context, should get the tuple at once
            new_ctx = Context(
                trace_id=ctx._parent_trace_id,
                span_id=ctx._parent_span_id,
                sampled=ctx._sampled,
            )
            new_ctx._current_span = ctx._current_span
            setattr(self, CONTEXT_ATTR, new_ctx)
module.py 文件源码 项目:dissonance 作者: jhgg 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def wrapper(o_fn):
            if timeout:
                f = functools.partial(gevent.with_timeout, timeout, o_fn, timeout_value=sync_ret_val)

            else:
                f = o_fn

            @functools.wraps(o_fn)
            def wrapped(*args, **kwargs):
                g = gevent.Greenlet(f, *args, **kwargs)
                g.link_exception(self._on_error)
                g.link(lambda v: self._running_greenlets.discard(g))
                self._running_greenlets.add(g)
                g.start()
                return sync_ret_val

            return wrapped
runtime.py 文件源码 项目:rill 作者: PermaData 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def start(self, graph_id, done_callback):
        """
        Execute a graph.
        """
        self.logger.debug('Graph {}: Starting execution'.format(graph_id))

        graph = self.get_graph(graph_id)

        network = Network(graph)
        executor = gevent.Greenlet(network.go)
        # FIXME: should we delete the executor from self._executors on finish?
        # this has an impact on the result returned from get_status().  Leaving
        # it means that after completion it will be started:True, running:False
        # until stop() is triggered, at which point it will be started:False,
        # running:False
        executor.link(lambda g: done_callback())
        self._executors[graph_id] = (executor, network)
        executor.start()
        # if executor.is_running():
        #     raise ValueError('Graph {} is already started'.format(graph_id))
endpoint.py 文件源码 项目:felix 作者: axbaretto 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def _interface_poll_loop(self):
        """Greenlet: Polls host endpoints for changes to their IP addresses.

        Sends updates to the EndpointManager via the _on_iface_ips_update()
        message.

        If polling is disabled, then it reads the interfaces once and then
        stops.
        """
        known_interfaces = {}
        while True:
            known_interfaces = self._poll_interfaces(known_interfaces)
            if self.config.HOST_IF_POLL_INTERVAL_SECS <= 0:
                _log.info("Host interface polling disabled, stopping after "
                          "initial read. Further changes to host endpoint "
                          "IPs will be ignored.")
                break
            gevent.sleep(self.config.HOST_IF_POLL_INTERVAL_SECS)
tests.py 文件源码 项目:raiden 作者: raiden-network 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def cleanup_tasks():
    tasks = [
        running_task
        for running_task in gc.get_objects()
        if isinstance(running_task, gevent.Greenlet)
    ]
    gevent.killall(tasks)
    gevent.hub.reinit()
17Actors.py 文件源码 项目:Python_Study 作者: thsheep 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __init__(self):
        self.inbox = Queue()
        gevent.Greenlet.__init__(self)
octp_server.py 文件源码 项目:octopus 作者: ideascf 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _start_watcher(self):
        """
        Start watcher coroutine for watch status of etcd.
        :return:
        :rtype: gevent.Greenlet
        """

        co = gevent.spawn(self._watcher_handler)
        log.info('watcher_handler(%s) started.', co)

        return co
octp_server.py 文件源码 项目:octopus 作者: ideascf 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def _start_heartbeat(self):
        """
        Start heartbeat coroutine for watch status of etcd.
        :return:
        :rtype: gevent.Greenlet
        """

        co = gevent.spawn(self._heartbeat_handler)
        log.info('watcher_handler(%s) started.', co)

        return co

    #### coroutine handler ####
patch.py 文件源码 项目:dd-trace-py 作者: DataDog 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def patch():
    """
    Patch the gevent module so that all references to the
    internal ``Greenlet`` class points to the ``DatadogGreenlet``
    class.

    This action ensures that if a user extends the ``Greenlet``
    class, the ``TracedGreenlet`` is used as a parent class.
    """
    _replace(TracedGreenlet)
    ddtrace.tracer.configure(context_provider=GeventContextProvider())
patch.py 文件源码 项目:dd-trace-py 作者: DataDog 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def unpatch():
    """
    Restore the original ``Greenlet``. This function must be invoked
    before executing application code, otherwise the ``DatadogGreenlet``
    class may be used during initialization.
    """
    _replace(__Greenlet)
    ddtrace.tracer.configure(context_provider=DefaultContextProvider())
patch.py 文件源码 项目:dd-trace-py 作者: DataDog 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _replace(g_class):
    """
    Utility function that replace the gevent Greenlet class with the given one.
    """
    # replace the original Greenlet class with the new one
    gevent.greenlet.Greenlet = g_class

    # replace gevent shortcuts
    gevent.Greenlet = gevent.greenlet.Greenlet
    gevent.spawn = gevent.greenlet.Greenlet.spawn
    gevent.spawn_later = gevent.greenlet.Greenlet.spawn_later
signal.py 文件源码 项目:oa_qian 作者: sunqb 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
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()
signal.py 文件源码 项目:RealtimePythonChat 作者: quangtqag 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
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()
concurrent.py 文件源码 项目:py-bson-rpc 作者: seprich 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _spawn_greenlet(fn, *args, **kwargs):
    from gevent import Greenlet
    g = Greenlet(fn, *args, **kwargs)
    g.start()
    return g
concurrent.py 文件源码 项目:py-bson-rpc 作者: seprich 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _spawn_greenlet(fn, *args, **kwargs):
    from gevent import Greenlet
    g = Greenlet(fn, *args, **kwargs)
    g.start()
    return g
note_scheduler.py 文件源码 项目:steppy 作者: ygravrand 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def start(self):
        g = gevent.Greenlet(self._start)
        g.start()
inputs.py 文件源码 项目:steppy 作者: ygravrand 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def activate_inputs(self):
        """Main activation method: launches a greenlet waiting for messages forever"""
        inputs_listener = gevent.Greenlet(self.receive_loop)
        inputs_listener.start()
        return inputs_listener
scheduler.py 文件源码 项目:steppy 作者: ygravrand 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def start(self):
        # print('*** Registering signal handlers ***')
        # self._register_signal_handlers()
        self.console.print_('*** Starting note scheduler ***')
        self.note_scheduler.start()
        self._target_time = self._initial_time = time.time()
        g = gevent.Greenlet(self._gevent_loop)
        g.start()
        g.join()
geventclient.py 文件源码 项目:arduino-ciao-meteor-ddp-connector 作者: andrea689 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __init__(self, url, protocols=None, extensions=None, ssl_options=None, headers=None):
        """
        WebSocket client that executes the
        :meth:`run() <ws4py.websocket.WebSocket.run>` into a gevent greenlet.

        .. code-block:: python

          ws = WebSocketClient('ws://localhost:9000/echo', protocols=['http-only', 'chat'])
          ws.connect()

          ws.send("Hello world")

          def incoming():
            while True:
               m = ws.receive()
               if m is not None:
                  print str(m)
               else:
                  break

          def outgoing():
            for i in range(0, 40, 5):
               ws.send("*" * i)

          greenlets = [
             gevent.spawn(incoming),
             gevent.spawn(outgoing),
          ]
          gevent.joinall(greenlets)
        """
        WebSocketBaseClient.__init__(self, url, protocols, extensions,
                                     ssl_options=ssl_options, headers=headers)
        self._th = Greenlet(self.run)

        self.messages = Queue()
        """
        Queue that will hold received messages.
        """
geventclient.py 文件源码 项目:wptagent 作者: WPO-Foundation 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def __init__(self, url, protocols=None, extensions=None, heartbeat_freq=None, ssl_options=None, headers=None):
        """
        WebSocket client that executes the
        :meth:`run() <ws4py.websocket.WebSocket.run>` into a gevent greenlet.

        .. code-block:: python

          ws = WebSocketClient('ws://localhost:9000/echo', protocols=['http-only', 'chat'])
          ws.connect()

          ws.send("Hello world")

          def incoming():
            while True:
               m = ws.receive()
               if m is not None:
                  print str(m)
               else:
                  break

          def outgoing():
            for i in range(0, 40, 5):
               ws.send("*" * i)

          greenlets = [
             gevent.spawn(incoming),
             gevent.spawn(outgoing),
          ]
          gevent.joinall(greenlets)
        """
        WebSocketBaseClient.__init__(self, url, protocols, extensions, heartbeat_freq,
                                     ssl_options=ssl_options, headers=headers)
        self._th = Greenlet(self.run)

        self.messages = Queue()
        """
        Queue that will hold received messages.
        """
test_it.py 文件源码 项目:salesforce-streaming-client 作者: SalesforceFoundation 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
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.greenlets.append(self.loop_greenlet)
        self.loop_greenlet.start()
redis.py 文件源码 项目:flask-sqlalchemy-socketio-demo 作者: lukeyeager 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def spawn(f):
            gevent.Greenlet(f).start()
zeromq.py 文件源码 项目:flask-sqlalchemy-socketio-demo 作者: lukeyeager 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def spawn(f):
            gevent.Greenlet(f).start()
tinybitcoinpeer.py 文件源码 项目:tinybitcoinpeer 作者: amiller 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def tee_and_handle(f, msgs):
    queue = Queue() # unbounded buffer
    def _run():
        for msg in msgs:
            print(COLOR_RECV, 'Received:', COLOR_ENDC, msg.command)
            if msg.command == b'ping':
                send(f, msg_pong(nonce=msg.nonce))
            queue.put(msg)
    t = gevent.Greenlet(_run)
    t.start()
    while True: yield(queue.get())
VirtualSensor.py 文件源码 项目:lc_cloud 作者: refractionPOINT 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def __init__( self, cloudDest, cbReceiveMessage, orgId, installerId, platform, architecture, 
                  sensorId = None, enrollmentToken = None, 
                  cbDebugLog = None, cbEnrollment = None ):
        gevent.Greenlet.__init__( self )
        self._cbDebugLog = cbDebugLog
        self._cbReceiveMessage = cbReceiveMessage
        self._cbEnrollment = cbEnrollment
        try:
            self._destServer, self._destPort = cloudDest.split( ':' )
        except:
            self._destServer = cloudDest
            self._destPort = 443
        self._oid = uuid.UUID( str( orgId ) )
        self._iid = uuid.UUID( str( installerId ) )
        self._sid = sensorId
        self._arch = architecture
        self._plat = platform
        if self._sid is not None:
            self._sid = uuid.UUID( str( self._sid ) )
        self._enrollmentToken = enrollmentToken
        self._socket = None

        self._threads = gevent.pool.Group()
        self._stopEvent = gevent.event.Event()
        self._lock = Semaphore( 1 )
        self._connectedEvent = gevent.event.Event()

        self._r = rpcm( isHumanReadable = True, isDebug = self._log )
        self._r.loadSymbols( Symbols.lookups )

        self._hcpModules = []
        self._hbsProfileHash = ( "\x00" * 32 )
test_14.py 文件源码 项目:notebook 作者: archever 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __init__(self):
        self.inbox = Queue()
        Greenlet.__init__(self)
signal.py 文件源码 项目:zenchmarks 作者: squeaky-pl 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
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()
module.py 文件源码 项目:dissonance 作者: jhgg 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _on_error(self, e):
        """
            Called when an error happens by something this module called.
        """
        if isinstance(e, gevent.Greenlet):
            e = e.exception

        self.client.on_module_error(self, e)
        logger.exception("Exception raised %r", e)
module.py 文件源码 项目:dissonance 作者: jhgg 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def spawn(self, f, *args, **kwargs):
        """
            Spawns a greenlet and does some book-keeping to make sure the greenlet is killed when the module is
            unloaded.
        """
        g = gevent.Greenlet(f, *args, **kwargs)
        g.link_exception(self._on_error)
        g.link(lambda v: self._running_greenlets.discard(g))
        self._running_greenlets.add(g)
        g.start()
        return g


问题


面经


文章

微信
公众号

扫码关注公众号