Example #1
0
def test_context_gevent(request):
    try:
        import gevent
    except ImportError:
        pytest.skip('gevent must be installed')

    request.addfinalizer(enable_gevent_context())

    def f1():
        assert Context.logging.flat == {}
        Context.logging.push({'f1': 1})
        Context.track('gevent', 1.0)
        assert Context.logging.flat == {'f1': 1}
        assert Context.current().tracking['gevent'].count == 1
        gevent.sleep(0.2)  # yield to let f2 run
        assert Context.logging.flat == {'f1': 1}
        assert Context.current().tracking['gevent'].count == 1

    def f2():
        assert Context.logging.flat == {}
        Context.logging.push({'f2': 2})
        Context.track('gevent', 1.0)
        assert Context.current().tracking['gevent'].count == 1
        assert Context.logging.flat == {'f2': 2}

    g1 = gevent.spawn(f1)
    g2 = gevent.spawn(f2)
    gevent.joinall([g1, g2], timeout=2)
Example #2
0
def run_gunicorn():
    config = get_config()

    # Early throw-away parsing of gunicorn config, as we need to decide
    # whether to enable prometheus multiprocess before we start importing
    from gunicorn.app.wsgiapp import WSGIApplication
    g_cfg = WSGIApplication().cfg

    # configure prometheus_client early as possible
    if pkg_is_installed('prometheus-client'):
        if g_cfg.workers > 1 or 'prometheus_multiproc_dir' in os.environ:
            from talisker.prometheus import setup_prometheus_multiproc
            async_workers = ('gevent', 'eventlet')
            # must be done before prometheus_client is imported *anywhere*
            setup_prometheus_multiproc(
                any(n in g_cfg.worker_class_str for n in async_workers))
    try:
        from gunicorn.workers.ggevent import GeventWorker
        from talisker.context import enable_gevent_context
    except Exception:
        pass
    else:
        if g_cfg.worker_class == GeventWorker:
            enable_gevent_context()

    try:
        from gunicorn.workers.geventlet import EventletWorker
        from talisker.context import enable_eventlet_context
    except Exception:
        pass
    else:
        if g_cfg.worker_class == EventletWorker:
            enable_eventlet_context()

    initialise()

    import talisker.gunicorn

    if pkg_is_installed('celery'):
        import talisker.celery
        talisker.celery.enable_signals()

    app = talisker.gunicorn.TaliskerApplication(
        "%(prog)s [OPTIONS] [APP_MODULE]", config.devel, config.debuglog)
    clear_context()
    return app.run()