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()
python类Greenlet()的实例源码
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)
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
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))
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)
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()
def __init__(self):
self.inbox = Queue()
gevent.Greenlet.__init__(self)
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
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 ####
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())
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())
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
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()
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()
def _spawn_greenlet(fn, *args, **kwargs):
from gevent import Greenlet
g = Greenlet(fn, *args, **kwargs)
g.start()
return g
def _spawn_greenlet(fn, *args, **kwargs):
from gevent import Greenlet
g = Greenlet(fn, *args, **kwargs)
g.start()
return g
def start(self):
g = gevent.Greenlet(self._start)
g.start()
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
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.
"""
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()
def spawn(f):
gevent.Greenlet(f).start()
def spawn(f):
gevent.Greenlet(f).start()
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())
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 )
def __init__(self):
self.inbox = Queue()
Greenlet.__init__(self)
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()
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)
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