本文整理匯總了Python中gevent.monkey方法的典型用法代碼示例。如果您正苦於以下問題:Python gevent.monkey方法的具體用法?Python gevent.monkey怎麽用?Python gevent.monkey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gevent
的用法示例。
在下文中一共展示了gevent.monkey方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: enable_async
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def enable_async():
global enabled
if enabled:
return enabled
if threading.active_count() > 3:
# This number used to be 1, but a gevent patch or something else changed this so it starts with 3 threads
logger.warning('{} threads already running. gvent monkey patching disabled...'.format(threading.active_count()))
enabled = False
else:
logger.debug('Monkey patching using gevent')
gevent.monkey.patch_all()
enabled = True
return enabled
示例2: green_threads
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def green_threads(self, val):
if val and socket.socket is not gevent.socket.socket:
warn("""
WARNING: green_threads is set but this process is
not monkey patched. This will cause severely degraded
performance.
CloudVolume uses gevent for cooperative (green)
threading but it requires patching the Python standard
library to perform asynchronous IO. Add this code to
the top of your program (before any other imports):
import gevent.monkey
gevent.monkey.patch_all(threads=False)
More Information:
http://www.gevent.org/intro.html#monkey-patching
""")
self.config.green = bool(val)
示例3: _unpatch_logging_handlers_lock
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def _unpatch_logging_handlers_lock():
# we dont want to use logger locks since those are used by both real thread and gevent greenlets
# switching from one to the other will cause gevent hub to throw an exception
import logging
RLock = gevent.monkey.saved['threading']['_CRLock']
for handler in logging._handlers.values():
if handler.lock:
handler.lock = RLock()
def create_unpatched_lock_for_handler(handler):
handler.lock = RLock()
# patch future handlers
logging.Handler.createLock = create_unpatched_lock_for_handler
示例4: main
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def main():
import gevent.monkey
gevent.monkey.patch_all()
from gevent.queue import LifoQueue
# These imports are inside the __main__ block
# to make sure that we only import from rq_gevent_worker
# (which has the side effect of applying gevent monkey patches)
# in the worker process. This way other processes can import the
# redis connection without that side effect.
import os
from redis import BlockingConnectionPool, StrictRedis
from rq import Queue, Connection
from dallinger.heroku.rq_gevent_worker import GeventWorker as Worker
from dallinger.config import initialize_experiment_package
initialize_experiment_package(os.getcwd())
import logging
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
redis_url = os.getenv("REDIS_URL", "redis://localhost:6379")
# Specify queue class for improved performance with gevent.
# see http://carsonip.me/posts/10x-faster-python-gevent-redis-connection-pool/
redis_pool = BlockingConnectionPool.from_url(redis_url, queue_class=LifoQueue)
redis_conn = StrictRedis(connection_pool=redis_pool)
with Connection(redis_conn):
worker = Worker(list(map(Queue, listen)))
worker.work()
示例5: _check_gevent
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def _check_gevent(expect_msg):
from _pydevd_bundle.pydevd_utils import notify_about_gevent_if_needed
assert not notify_about_gevent_if_needed()
import gevent
assert not notify_about_gevent_if_needed()
import gevent.monkey
assert not notify_about_gevent_if_needed()
gevent.monkey.patch_all()
assert notify_about_gevent_if_needed() == expect_msg
示例6: _cli_patch
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def _cli_patch(cli_args): # pragma: no coverage
parsed_args, _ = _cli_parse(cli_args)
opts = parsed_args
if opts.server:
if opts.server.startswith('gevent'):
import gevent.monkey
gevent.monkey.patch_all()
elif opts.server.startswith('eventlet'):
import eventlet
eventlet.monkey_patch()
示例7: run
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def run(self, handler):
from gevent import pywsgi, local
if not isinstance(threading.local(), local.local):
msg = "Bottle requires gevent.monkey.patch_all() (before import)"
raise RuntimeError(msg)
if self.quiet:
self.options['log'] = None
address = (self.host, self.port)
server = pywsgi.WSGIServer(address, handler, **self.options)
if 'BOTTLE_CHILD' in os.environ:
import signal
signal.signal(signal.SIGINT, lambda s, f: server.stop())
server.serve_forever()
示例8: spawn
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def spawn(mode, func, *args, **kwargs):
"""Spawns a thread-like object which runs the given function concurrently.
Available modes:
- `threading`
- `greenlet`
- `eventlet`
"""
if mode is None:
# 'threading' is the default mode.
mode = 'threading'
elif mode not in spawn.modes:
# validate the given mode.
raise ValueError('Invalid spawn mode: %s' % mode)
if mode == 'threading':
return spawn_thread(func, *args, **kwargs)
elif mode == 'gevent':
import gevent
import gevent.monkey
gevent.monkey.patch_select()
gevent.monkey.patch_socket()
return gevent.spawn(func, *args, **kwargs)
elif mode == 'eventlet':
import eventlet
eventlet.patcher.monkey_patch(select=True, socket=True)
return eventlet.spawn(func, *args, **kwargs)
assert False
示例9: check_gevent_monkey_patch
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def check_gevent_monkey_patch(raise_exc=True):
if not monkey.is_module_patched('socket'): # 隨便選一個檢測標誌
if raise_exc:
warnings.warn(f'檢測到 你還沒有打gevent包的猴子補丁,請在所運行的起始腳本第一行寫上 【import gevent.monkey;gevent.monkey.patch_all()】 這句話。')
raise Exception(f'檢測到 你還沒有打gevent包的猴子補丁,請在所運行的起始腳本第一行寫上 【import gevent.monkey;gevent.monkey.patch_all()】 這句話。')
else:
return 1
示例10: _cli_patch
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def _cli_patch(args):
opts, _, _ = _cli_parse(args)
if opts.server:
if opts.server.startswith('gevent'):
import gevent.monkey
gevent.monkey.patch_all()
elif opts.server.startswith('eventlet'):
import eventlet
eventlet.monkey_patch()
示例11: run_items
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def run_items(self, items, session, workers=None):
import gevent
import gevent.monkey
import gevent.pool
gevent.monkey.patch_all()
pool = gevent.pool.Pool(size=workers)
for index, item in enumerate(items):
pool.spawn(self._run_next_item, session, item, index)
pool.join()
示例12: run
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def run(self, handler):
from gevent import wsgi, pywsgi, local
if not isinstance(threading.local(), local.local):
msg = "Bottle requires gevent.monkey.patch_all() (before import)"
raise RuntimeError(msg)
if not self.options.pop('fast', None): wsgi = pywsgi
self.options['log'] = None if self.quiet else 'default'
address = (self.host, self.port)
server = wsgi.WSGIServer(address, handler, **self.options)
if 'BOTTLE_CHILD' in os.environ:
import signal
signal.signal(signal.SIGINT, lambda s, f: server.stop())
server.serve_forever()
示例13: apply_patch
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def apply_patch(hogging_detection=False, real_threads=1):
_logger.info('applying gevent patch (%s real threads)', real_threads)
# real_threads is 1 by default so it will be possible to run watch_threads concurrently
if hogging_detection:
real_threads += 1
if real_threads:
_RealThreadsPool(real_threads)
_patch_module_locks()
import gevent
import gevent.monkey
for m in ["easypy.threadtree", "easypy.concurrency"]:
assert m not in sys.modules, "Must apply the gevent patch before importing %s" % m
gevent.monkey.patch_all(Event=True, sys=True)
_unpatch_logging_handlers_lock()
global HUB
HUB = gevent.get_hub()
global threading
import threading
for thread in threading.enumerate():
_set_thread_uuid(thread.ident)
_set_main_uuid() # the patched threading has a new ident for the main thread
# this will declutter the thread dumps from gevent/greenlet frames
from .threadtree import _BOOTSTRAPPERS
import gevent, gevent.threading, gevent.greenlet
_BOOTSTRAPPERS.update([gevent, gevent.threading, gevent.greenlet])
if hogging_detection:
import greenlet
greenlet.settrace(lambda *args: _greenlet_trace_func(*args))
defer_to_thread(detect_hogging, 'detect-hogging')
示例14: _patch_module_locks
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def _patch_module_locks():
# gevent will not patch existing locks (including ModuleLocks) when it's not single threaded
# our solution is to monkey patch the release method for ModuleLocks objects
# we assume that patching is done early enough so no other locks are present
import importlib
_old_release = importlib._bootstrap._ModuleLock.release
def _release(*args, **kw):
lock = args[0]
if lock.owner == main_thread_ident_before_patching:
lock.owner = threading.main_thread().ident
_old_release(*args, **kw)
importlib._bootstrap._ModuleLock.release = _release
示例15: non_gevent_sleep
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import monkey [as 別名]
def non_gevent_sleep(timeout):
try:
gevent.monkey.saved['time']['sleep'](timeout)
except KeyError:
time.sleep(timeout)