本文整理汇总了Python中gevent.monkey.patch_all方法的典型用法代码示例。如果您正苦于以下问题:Python monkey.patch_all方法的具体用法?Python monkey.patch_all怎么用?Python monkey.patch_all使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.monkey
的用法示例。
在下文中一共展示了monkey.patch_all方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: patch
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def patch(self):
from gevent import monkey
monkey.noisy = False
# if the new version is used make sure to patch subprocess
if gevent.version_info[0] == 0:
monkey.patch_all()
else:
monkey.patch_all(subprocess=True)
# monkey patch sendfile to make it none blocking
patch_sendfile()
# patch sockets
sockets = []
for s in self.sockets:
if sys.version_info[0] == 3:
sockets.append(socket(s.FAMILY, _socket.SOCK_STREAM,
fileno=s.sock.fileno()))
else:
sockets.append(socket(s.FAMILY, _socket.SOCK_STREAM,
_sock=s))
self.sockets = sockets
示例2: testGevent
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def testGevent():
# 创建一个协程对象,创建的时候就自动运行了,不需要手动启动
g1 = gevent.spawn(work5, 10)
g2 = gevent.spawn(work5, 10)
g3 = gevent.spawn(work5, 10)
# 阻塞等待协程执行完毕
# 没使用monkey.patch_all()破解的时候不会自动切换,破解后就会随机协程
# g1.join()
# g2.join()
# g3.join()
# 程序从上到下执行,不管之前有没有异步代码,遇到join相当于一面墙,堵住下面的路
gevent.joinall([g1,g2,g3])
print("全部协程结束完毕.")
示例3: run
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def run(self):
import gevent
from gevent import monkey
monkey.patch_all()
from gevent import pool
# default 200
# g_pool = pool.Pool(200)
g_pool = pool.Pool(self.coroutine)
tasks = [g_pool.spawn(self.gen_traffic, url) for url in self.url_list]
gevent.joinall(tasks)
traffic_list = []
for i in tasks:
if i.value is not None:
traffic_list.append(i.value)
# save traffic for rescan
Engine.save_traffic(traffic_list, self.id)
示例4: verify_async
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def verify_async(case_list,coroutine):
"""
Verify used gevent lib
:param case_list:
:param coroutine:
:return:
"""
from gevent import monkey
monkey.patch_all()
result = []
geventPool = pool.Pool(coroutine)
tasks = [geventPool.spawn(Verify.request_and_verify, case) for case in case_list]
gevent.joinall(tasks)
for i in tasks:
if i.value is not None:
result.append(i.value)
print_info('Total Verify-Case is: %s, %s error happened.' % (len(case_list), Verify.ERROR_COUNT))
return result
示例5: patch
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def patch(self):
from gevent import monkey
monkey.noisy = False
# if the new version is used make sure to patch subprocess
if gevent.version_info[0] == 0:
monkey.patch_all()
else:
monkey.patch_all(subprocess=True)
# monkey patch sendfile to make it none blocking
patch_sendfile()
# patch sockets
sockets = []
for s in self.sockets:
sockets.append(socket(s.FAMILY, _socket.SOCK_STREAM,
_sock=s))
self.sockets = sockets
示例6: run
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def run(servername, ip, port, softcron=True, logging=False, profiler=None,
options=None):
if servername == 'gevent':
from gevent import monkey
monkey.patch_all()
elif servername == 'eventlet':
import eventlet
eventlet.monkey_patch()
import gluon.main
if logging:
application = gluon.main.appfactory(wsgiapp=gluon.main.wsgibase,
logfilename='httpserver.log',
profiler_dir=profiler)
else:
application = gluon.main.wsgibase
if softcron:
from gluon.settings import global_settings
global_settings.web2py_crontype = 'soft'
getattr(Servers, servername)(application, (ip, int(port)), options=options)
示例7: run
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def run():
initEngine()
if conf.ENGINE is ENGINE_MODE_STATUS.THREAD: #多线程模式conf.ENGINE=9
for i in range(th.threads_num):
t = threading.Thread(target=scan, name=str(i))
setThreadDaemon(t)
t.start()
# It can quit with Ctrl-C
while 1: #如果未扫描结束,主线程一直死循环等待,取决于线程数量和th.is_continue的值
if th.thread_count > 0 and th.is_continue:
time.sleep(0.01)
else:
break
elif conf.ENGINE is ENGINE_MODE_STATUS.GEVENT: #协程模式conf.ENGINE=8
from gevent import monkey
monkey.patch_all()
import gevent
while th.queue.qsize() > 0 and th.is_continue:
gevent.joinall([gevent.spawn(scan) for i in xrange(0, th.threads_num) if #生成10个concurrent
th.queue.qsize() > 0])
if 'errmsg' in th:
logger.error(th.errmsg)
示例8: start_server
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def start_server():
logger.info(f"server port: {config.SERVER_PORT}")
logger.info(f"pic root dir path: {config.PIC_DIR_PATH}")
# check existed
assert os.path.exists(
config.PIC_DIR_PATH
), f"dir path not existed: {config.PIC_DIR_PATH}"
from gevent import monkey, pywsgi
monkey.patch_all()
server = pywsgi.WSGIServer(("0.0.0.0", int(config.SERVER_PORT)), app)
server.serve_forever()
示例9: run
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def run(self, handler):
from gevent import wsgi as wsgi_fast, pywsgi, monkey, local
if self.options.get('monkey', True):
if not threading.local is local.local: monkey.patch_all()
wsgi = wsgi_fast if self.options.get('fast') else pywsgi
wsgi.WSGIServer((self.host, self.port), handler).serve_forever()
示例10: test_notify_gevent
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def test_notify_gevent(case_setup, pyfile):
def get_environ(writer):
# I.e.: Make sure that gevent support is disabled
env = os.environ.copy()
env['GEVENT_SUPPORT'] = ''
return env
@pyfile
def case_gevent():
from gevent import monkey
monkey.patch_all()
print('TEST SUCEEDED')
def additional_output_checks(writer, stdout, stderr):
assert 'environment variable' in stderr
assert 'GEVENT_SUPPORT=True' in stderr
with case_setup.test_file(
case_gevent,
get_environ=get_environ,
additional_output_checks=additional_output_checks,
EXPECTED_RETURNCODE='any',
FORCE_KILL_PROCESS_WHEN_FINISHED_OK=True
) as writer:
json_facade = JsonFacade(writer)
json_facade.write_launch()
json_facade.write_make_initial_run()
wait_for_condition(lambda: 'GEVENT_SUPPORT=True' in writer.get_stderr())
writer.finished_ok = True
示例11: patch_os
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def patch_os():
"""
Replace :func:`os.fork` with :func:`gevent.fork`, and, on POSIX,
:func:`os.waitpid` with :func:`gevent.os.waitpid` (if the
environment variable ``GEVENT_NOWAITPID`` is not defined). Does
nothing if fork is not available.
This method must be used with :func:`patch_signal` to have proper SIGCHLD
handling. :func:`patch_all` calls both by default.
"""
patch_module('os')
示例12: patch_signal
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def patch_signal():
"""
Make the signal.signal function work with a monkey-patched os.
This method must be used with :func:`patch_os` to have proper SIGCHLD
handling. :func:`patch_all` calls both by default.
.. seealso:: :mod:`gevent.signal`
"""
patch_module("signal")
示例13: main
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def main():
args = {}
argv = sys.argv[1:]
verbose = False
script_help, patch_all_args, modules = _get_script_help()
while argv and argv[0].startswith('--'):
option = argv[0][2:]
if option == 'verbose':
verbose = True
elif option.startswith('no-') and option.replace('no-', '') in patch_all_args:
args[option[3:]] = False
elif option in patch_all_args:
args[option] = True
if option in modules:
for module in modules:
args.setdefault(module, False)
else:
sys.exit(script_help + '\n\n' + 'Cannot patch %r' % option)
del argv[0]
# TODO: break on --
if verbose:
import pprint
import os
print('gevent.monkey.patch_all(%s)' % ', '.join('%s=%s' % item for item in args.items()))
print('sys.version=%s' % (sys.version.strip().replace('\n', ' '), ))
print('sys.path=%s' % pprint.pformat(sys.path))
print('sys.modules=%s' % pprint.pformat(sorted(sys.modules.keys())))
print('cwd=%s' % os.getcwd())
patch_all(**args)
if argv:
sys.argv = argv
__package__ = None
assert __package__ is None
globals()['__file__'] = sys.argv[0] # issue #302
with open(sys.argv[0]) as f:
exec(f.read())
else:
print(script_help)
示例14: _get_script_help
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [as 别名]
def _get_script_help():
from inspect import getargspec
patch_all_args = getargspec(patch_all)[0]
modules = [x for x in patch_all_args if 'patch_' + x in globals()]
script_help = """gevent.monkey - monkey patch the standard modules to use gevent.
USAGE: python -m gevent.monkey [MONKEY OPTIONS] script [SCRIPT OPTIONS]
If no OPTIONS present, monkey patches all the modules it can patch.
You can exclude a module with --no-module, e.g. --no-thread. You can
specify a module to patch with --module, e.g. --socket. In the latter
case only the modules specified on the command line will be patched.
MONKEY OPTIONS: --verbose %s""" % ', '.join('--[no-]%s' % m for m in modules)
return script_help, patch_all_args, modules
示例15: check_gevent_monkey_patch
# 需要导入模块: from gevent import monkey [as 别名]
# 或者: from gevent.monkey import patch_all [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