def start(): flag = False try: from gevent import monkey flag = monkey.is_module_patched("socket") except ModuleNotFoundError: logger.debug( "it was found that no gevent was used, if you don't use, please ignore." ) if flag: import grpc.experimental.gevent as grpc_gevent grpc_gevent.init_gevent() global __started if __started: raise RuntimeError('the agent can only be started once') loggings.init() config.finalize() __started = True __init() __heartbeat_thread.start() __report_thread.start() atexit.register(__fini)
def patch(self): """Override `patch` so that we can patch some extra libraries. Gevent patches base libraries that perform I/O for us already (e.g. `os` and `sockets). We need to patch any additional libraries that perform I/O outside of the base libraries in order to make them cooperative. See https://docs.gunicorn.org/en/stable/design.html#async-workers and https://docs.gunicorn.org/en/stable/design.html#async-workers. For a longer discussion of why this is important, see description in #3850. """ # pylint: disable=import-outside-toplevel super().patch() # patch grpc from grpc.experimental import gevent as grpc_gevent grpc_gevent.init_gevent() self.log.info('patched grpc') # patch psycopg2 from psycogreen.gevent import patch_psycopg patch_psycopg() self.log.info('patched psycopg2')
# Tweak gevent and grpc so they work together: # https://github.com/grpc/grpc/issues/4629#issuecomment-376962677 # Make the standard library cooperate with gevent. # http://www.gevent.org/api/gevent.monkey.html from gevent import monkey monkey.patch_all() # This must be called AFTER the python standard lib has been patched, but BEFORE creating and gRPC objects. # https://github.com/grpc/grpc/blob/v1.32.x/src/python/grpcio/grpc/experimental/gevent.py import grpc.experimental.gevent as grpc_gevent grpc_gevent.init_gevent()
def patch(self): super().patch() grpc_gevent.init_gevent()
def patch(self): super(GeventGrpcWorker, self).patch() grpc_gevent.init_gevent() self.log.info('patched grpc')
import inspect import logging import os import random import time from optparse import OptionParser import gevent; from gevent import monkey; monkey.patch_all() import requests from locust import HttpLocust from httpsession import HttpSession from stats import Stats, StackDriverStats import grpc.experimental.gevent as grpc_gevent; grpc_gevent.init_gevent() logging.basicConfig( format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S') log = logging.getLogger(__name__) TARGET_HOST = os.getenv('TARGET_HOST', 'http://localhost:5000') NUM_USERS = int(os.getenv('NUM_USERS', '1')) MODE_CONTINUOUS = 'continuous' MODE_AFTER_DEPLOY = 'after_deploy' MODE_ONE_OFF = 'one_off' MODE = os.getenv('MODE', MODE_CONTINUOUS)
from gevent.monkey import patch_all patch_all() try: import grpc except ImportError: pass else: if grpc.__version__ != '1.18.0': raise Exception('grpcio version must be 1.18.0') from grpc.experimental import gevent gevent.init_gevent()