File: monitoring.rst

package info (click to toggle)
python-gevent 25.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,036 kB
  • sloc: python: 170,894; ansic: 82,360; sh: 6,265; makefile: 1,550; javascript: 108
file content (108 lines) | stat: -rw-r--r-- 3,861 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
==============================================
 Monitoring and Debugging gevent Applications
==============================================

gevent applications are often long-running server processes. Beginning
with version 1.3, gevent has special support for monitoring such
applications and getting visibility into them.

.. tip::

   For some additional tools, see the comments on `issue 1021
   <https://github.com/gevent/gevent/issues/1021>`_.

The Monitor Thread
==================

gevent can be :attr:`configured
<gevent._config.Config.monitor_thread>` to start a native thread to
watch over each hub it creates. Out of the box, that thread has
support to watch two things, but you can :func:`add your own functions
<gevent.events.IPeriodicMonitorThread.add_monitoring_function>` to be
called periodically in this thread.

Blocking
--------

When the monitor thread is enabled, by default it will watch for
greenlets that block the event loop for longer than a
:attr:`configurable <gevent._config.Config.max_blocking_time>` time
interval. When such a blocking greenlet is detected, it will print
:func:`a report <gevent.util.format_run_info>` to the hub's
:attr:`~gevent.hub.Hub.exception_stream`. It will also emit the
:class:`gevent.events.EventLoopBlocked` event.

.. seealso:: :func:`gevent.util.assert_switches`

   For a scoped version of this.

Memory Usage
------------

Optionally, you can set a :attr:`memory limit
<gevent._config.Config.max_memory_usage>`. The monitor thread will
check the process's memory usage every
:attr:`~gevent._config.Config.memory_monitor_period` seconds, and if
it is found to exceed this value, the
:class:`gevent.events.MemoryUsageThresholdExceeded` event will be
emitted. If in the future memory usage declines below the configured
value, the :class:`gevent.events.MemoryUsageUnderThreshold` event will
be emitted.

.. important::

   `psutil <https://pypi.org/project/psutil>`_ must be
   installed to monitor memory usage.

Visibility
==========

.. tip::

   Insight into the monkey-patching process can be obtained by
   observing the events :mod:`gevent.monkey` emits.

It is sometimes useful to get an overview of all existing greenlets
and their stack traces. The function
:func:`gevent.util.print_run_info` will collect this info and print it
(:func:`gevent.util.format_run_info` only collects and returns this
information). The greenlets are organized into a tree based on the
greenlet that spawned them.

The ``print_run_info`` function is commonly hooked up to a signal
handler to get the application state at any given time.

For each greenlet the following information is printed:

- Its current execution stack
- If it is not running, its termination status and
  :attr:`gevent.Greenlet.value` or
  :attr:`gevent.Greenlet.exception`
- The :attr:`stack at which it was spawned
  <gevent.Greenlet.spawning_stack>`
- Its parent (usually the hub)
- Its :attr:`~gevent.Greenlet.minimal_ident`
- Its :attr:`~gevent.Greenlet.name`
- The :attr:`spawn tree locals <gevent.Greenlet.spawn_tree_locals>`
  (only for the root of the spawn tree).
- The dicts of all :class:`gevent.local.local` objects that are used
  in the greenlet.

The greenlet tree itself is represented as an object that you can also
use for your own purposes: :class:`gevent.util.GreenletTree`.

Profiling
=========

The github repository `nylas/nylas-perftools
<https://github.com/nylas/nylas-perftools>`_ has some
gevent-compatible profilers.

- ``stacksampler`` is a sampling profiler meant to be run in a
  greenlet in your server process and exposes data through an HTTP
  server; it is designed to be suitable for production usage.
- ``py2devtools`` is a greenlet-aware tracing profiler that outputs data
  that can be used by the Chrome dev tools; it is intended for
  developer usage.

..  LocalWords:  greenlets gevent greenlet