New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Allow API-based control of gevent-wide behaviors without env vars #1090

Closed
arcivanov opened this Issue Feb 2, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@arcivanov
Contributor

arcivanov commented Feb 2, 2018

Quite a few features of Gevent rely on setting env vars, such as DNS resolver, thread pool, etc.
This gets very cumbersome when monkeypatch bootstrap is used, pollutes process's own os.environ with GEVENT settings that would be inherited by children by default, which is not always desirable.

Ideally, gevent would have two methods of configuring global behavioral settings that compliment each other and allow configuring from API.

Possible example (not advocating any particular module/naming convention):

from gevent import settings

current_setting = settings.get_gevent_xxx()
settings.set_gevent_xxx(classname_or_str_or_value)

from gevent.monkey import ...

The get_gevent_xxx would first check if the setting was set via API, then via environ, then assume default.

Another advantage is that settings being aggregated that way in one place are easier to document. Right now it's almost impossible to be sure you found documentation on all of the configurable behaviors of gevent - have to grep repo code by hand.

jamadden added a commit that referenced this issue Feb 12, 2018

Centralize configuration in a ``gevent.config`` object.
This lets us document things more clearly, and, with care, allows
configuring gevent from Python code without any environment varibles.

All the fileobject implementations needed to be moved out of the
``gevent.fileobject`` module so as not to introduce import cycles that
made configuring from code impossible.

Fixes #1090
@jamadden

This comment has been minimized.

Member

jamadden commented Feb 12, 2018

I admit I was a little reluctant about this idea: I didn't like the fact that it put all these non-local concerns in one place (e.g., the only code that cares about GEVENT_NOWAITPID is in gevent/os.py; why should that knowledge be spread anywhere else?). But the more I thought about it, the more I came to feel that the benefits would outweigh that minor smell.

PR #1096 lets you do this:

>>> from gevent import config
>>> config.loop = 'libuv'
>>> config.fileobject = 'thread'
>>> import gevent.core, gevent.fileobject
>>> gevent.core.loop
<class 'gevent.libuv.loop.loop'>
>>> gevent.filegevent.fileobject.FileObject
<class 'gevent._fileobjectcommon.FileObjectThread'>

jamadden added a commit that referenced this issue Feb 12, 2018

Centralize configuration in a ``gevent.config`` object.
This lets us document things more clearly, and, with care, allows
configuring gevent from Python code without any environment varibles.

All the fileobject implementations needed to be moved out of the
``gevent.fileobject`` module so as not to introduce import cycles that
made configuring from code impossible.

Fixes #1090
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment