Configuring gevent

See also

gevent.setswitchinterval() For additional runtime configuration.

class Config[source]

Bases: object

Global configuration for gevent.

There is one instance of this object at gevent.config. If you are going to make changes in code, instead of using the documented environment variables, you need to make the changes before using any parts of gevent that might need those settings. For example:

>>> from gevent import config
>>> config.fileobject = 'thread'

>>> from gevent import fileobject
>>> fileobject.FileObject.__name__
'FileObjectThread'

New in version 1.3a2.

property ares_servers

A list of strings giving the IP addresses of nameservers for the ares resolver.

In the environment variable, these strings are separated by commas.

Deprecated since version 1.3a2: Prefer the resolver_nameservers setting. If both are set, the results are not defined.

The default value is None

The environment variable GEVENTARES_SERVERS can be used to control this.

property ares_timeout

Deprecated since version 1.3a2: Prefer the resolver_timeout setting. If both are set, the results are not defined.

The default value is None

The environment variable GEVENTARES_TIMEOUT can be used to control this.

property disable_watch_children

Should we not watch children with the event loop watchers?

This is an advanced setting.

See gevent.os for a detailed description.

This is a boolean value.

In the environment variable, it may be given as 1, true, on or yes for True, or 0, false, off, or no for False.

The default value is False

The environment variable GEVENT_NOWAITPID can be used to control this.

property fileobject

The kind of FileObject we will use.

See gevent.fileobject for a detailed description.

This is an importable value. It can be given as a string naming an importable object, or a list of strings in preference order and the first successfully importable object will be used. (Separate values in the environment variable with commas.) It can also be given as the callable object itself (in code). Shorthand names for default objects are [‘thread’, ‘posix’, ‘block’]

The default value is ['posix', 'thread']

The environment variable GEVENT_FILE can be used to control this.

property format_context

This is an importable value. It can be given as a string naming an importable object, or a list of strings in preference order and the first successfully importable object will be used. (Separate values in the environment variable with commas.) It can also be given as the callable object itself (in code).

The default value is pprint.saferepr

The environment variable GEVENT_FORMAT_CONTEXT can be used to control this.

property libev_backend

The backend for libev, such as ‘select’

The default value is None

The environment variable GEVENT_BACKEND can be used to control this.

property loop

The kind of the loop we use.

On Windows, this defaults to libuv, while on other platforms it defaults to libev.

This is an importable value. It can be given as a string naming an importable object, or a list of strings in preference order and the first successfully importable object will be used. (Separate values in the environment variable with commas.) It can also be given as the callable object itself (in code). Shorthand names for default objects are [‘libev-cext’, ‘libev-cffi’, ‘libuv-cffi’, ‘libuv’]

The default value is ['libev-cext', 'libev-cffi', 'libuv-cffi']

The environment variable GEVENT_LOOP can be used to control this.

property max_blocking_time

If the monitor_thread is enabled, this is approximately how long (in seconds) the event loop will be allowed to block before a warning is issued.

This function depends on using greenlet.settrace, so installing your own trace function after starting the monitoring thread will cause this feature to misbehave unless you call the function returned by greenlet.settrace. If you install a tracing function before the monitoring thread is started, it will still be called.

Note

In the unlikely event of creating and using multiple different gevent hubs in the same native thread in a short period of time, especially without destroying the hubs, false positives may be reported.

New in version 1.3b1.

The default value is 0.1

The environment variable GEVENT_MAX_BLOCKING_TIME can be used to control this.

property max_memory_usage

If monitor_thread is enabled, then if memory usage exceeds this amount (in bytes), events will be emitted. See gevent.events. In the environment variable, you can use a suffix of ‘kb’, ‘mb’ or ‘gb’ to specify the value in kilobytes, megabytes or gigibytes.

There is no default value for this setting. If you wish to cap memory usage, you must choose a value.

The default value is None

The environment variable GEVENT_MONITOR_MEMORY_MAX can be used to control this.

property memory_monitor_period

If monitor_thread is enabled, this is approximately how long (in seconds) we will go between checking the processes memory usage.

Checking the memory usage is relatively expensive on some operating systems, so this should not be too low. gevent will place a floor value on it.

The default value is 5

The environment variable GEVENT_MONITOR_MEMORY_PERIOD can be used to control this.

property monitor_thread

Should each hub start a native OS thread to monitor for problems?

Such a thread will periodically check to see if the event loop is blocked for longer than max_blocking_time, producing output on the hub’s exception stream (stderr by default) if it detects this condition.

If this setting is true, then this thread will be created the first time the hub is switched to, or you can call gevent.hub.Hub.start_periodic_monitoring_thread() at any time to create it (from the same thread that will run the hub). That function will return an instance of gevent.events.IPeriodicMonitorThread to which you can add your own monitoring functions. That function also emits an event of gevent.events.PeriodicMonitorThreadStartedEvent.

New in version 1.3b1.

This is a boolean value.

In the environment variable, it may be given as 1, true, on or yes for True, or 0, false, off, or no for False.

The default value is False

The environment variable GEVENT_MONITOR_THREAD_ENABLE can be used to control this.

property resolver

The callable that will be used to create gevent.hub.Hub.resolver.

See Name Resolution (DNS) for more information.

This is an importable value. It can be given as a string naming an importable object, or a list of strings in preference order and the first successfully importable object will be used. (Separate values in the environment variable with commas.) It can also be given as the callable object itself (in code). Shorthand names for default objects are [‘ares’, ‘thread’, ‘block’, ‘dnspython’]

The default value is ['thread', 'dnspython', 'ares', 'block']

The environment variable GEVENT_RESOLVER can be used to control this.

property resolver_nameservers

A list of strings giving the IP addresses of nameservers for the (non-system) resolver.

In the environment variable, these strings are separated by commas.

Resolver Behaviour

  • blocking

    Ignored

  • Threaded

    Ignored

  • dnspython

    If this setting is not given, the dnspython resolver will load nameservers to use from /etc/resolv.conf or the Windows registry. This setting replaces any nameservers read from those means. Note that the file and registry are still read for other settings.

    Caution

    dnspython does not validate the members of the list. An improper address (such as a hostname instead of IP) has undefined results, including hanging the process.

  • ares

    Similar to dnspython, but with more platform and compile-time options. ares validates that the members of the list are valid addresses.

The default value is None

The environment variable GEVENT_RESOLVER_NAMESERVERS can be used to control this.

property resolver_timeout

The total amount of time that the DNS resolver will spend making queries.

Only the ares and dnspython resolvers support this.

New in version 1.3a2.

The default value is None

The environment variable GEVENT_RESOLVER_TIMEOUT can be used to control this.

property threadpool

The kind of threadpool we use.

This is an importable value. It can be given as a string naming an importable object, or a list of strings in preference order and the first successfully importable object will be used. (Separate values in the environment variable with commas.) It can also be given as the callable object itself (in code).

The default value is gevent.threadpool.ThreadPool

The environment variable GEVENT_THREADPOOL can be used to control this.

property threadpool_idle_task_timeout

How long threads in the default threadpool (used for DNS by default) are allowed to be idle before exiting.

Use -1 for no timeout.

New in version 22.08.0.

The default value is 5.0

The environment variable GEVENT_THREADPOOL_IDLE_TASK_TIMEOUT can be used to control this.

property trace_malloc

Should FFI objects track their allocation?

This is only useful for low-level debugging.

On Python 3, this environment variable is built in to the interpreter, and it may also be set with the -X tracemalloc command line argument.

On Python 2, gevent interprets this argument and adds extra tracking information for FFI objects.

The default value is False

The environment variable PYTHONTRACEMALLOC can be used to control this.

property track_greenlet_tree

Should Greenlet objects track their spawning tree?

Setting this to a false value will make spawning Greenlet objects and using spawn_raw faster, but the spawning_greenlet, spawn_tree_locals and spawning_stack will not be captured. Setting this to a false value can also reduce memory usage because capturing the stack captures some information about Python frames.

New in version 1.3b1.

This is a boolean value.

In the environment variable, it may be given as 1, true, on or yes for True, or 0, false, off, or no for False.

The default value is True

The environment variable GEVENT_TRACK_GREENLET_TREE can be used to control this.

Next page: API reference