Skip to content

Commit 892be4c

Browse files
authored
Merge pull request #2098 from gevent/issue2070
Add the 'print_blocking_reports'/'GEVENT_MONITOR_PRINT_BLOCKING_REPOR…TS' setting to control whether blocking reports are printed.
2 parents 72dba27 + 71dbac7 commit 892be4c

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

docs/changes/2070.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add a new environment variable and configuration setting to control
2+
whether blocking reports are printed by the monitor thread.

src/gevent/_config.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
This is handy to quickly look for environment variables.
1414
"""
1515

16-
from __future__ import print_function, absolute_import, division
17-
1816
import importlib
1917
import os
2018
import textwrap
@@ -116,7 +114,7 @@ def validate_anything(value):
116114

117115
convert_str_value_as_is = validate_anything
118116

119-
class Setting(object):
117+
class Setting(metaclass=SettingType):
120118
name = None
121119
value = None
122120
validate = staticmethod(validate_invalid)
@@ -155,7 +153,6 @@ def set(self, val):
155153
self.value = self.validate(self._convert(val))
156154

157155

158-
Setting = SettingType('Setting', (Setting,), dict(Setting.__dict__))
159156

160157
def make_settings():
161158
"""
@@ -176,7 +173,8 @@ class Config(object):
176173
There is one instance of this object at ``gevent.config``. If you
177174
are going to make changes in code, instead of using the documented
178175
environment variables, you need to make the changes before using
179-
any parts of gevent that might need those settings. For example::
176+
any parts of gevent that might need those settings (unless otherwise
177+
documented). For example::
180178
181179
>>> from gevent import config
182180
>>> config.fileobject = 'thread'
@@ -351,6 +349,7 @@ class Threadpool(ImportableSetting, Setting):
351349

352350
default = 'gevent.threadpool.ThreadPool'
353351

352+
354353
class ThreadpoolIdleTaskTimeout(FloatSettingMixin, Setting):
355354
document = True
356355
name = 'threadpool_idle_task_timeout'
@@ -546,6 +545,24 @@ class MaxBlockingTime(FloatSettingMixin, Setting):
546545
.. versionadded:: 1.3b1
547546
"""
548547

548+
549+
class PrintBlockingReports(BoolSettingMixin, Setting):
550+
name = 'print_blocking_reports'
551+
default = True
552+
553+
environment_key = 'GEVENT_MONITOR_PRINT_BLOCKING_REPORTS'
554+
desc = """\
555+
If `monitor_thread` is enabled, and gevent detects a hub blocked
556+
for more than `max_blocking_time`, should gevent print a detailed
557+
report about the block?
558+
559+
The report is generated and notifications are broadcast whether
560+
or not the report is printed.
561+
562+
.. versionadded:: NEXT
563+
"""
564+
565+
549566
class MonitorMemoryPeriod(FloatSettingMixin, Setting):
550567
name = 'memory_monitor_period'
551568

src/gevent/_monitor.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,13 @@ def monitor_blocking(self, hub):
252252

253253

254254
def _show_blocking_report(self, hub, report, active_greenlet):
255-
stream = hub.exception_stream
256-
for line in report:
257-
# Printing line by line may interleave with other things,
258-
# but it should also prevent a "reentrant call to print"
259-
# when the report is large.
260-
print(line, file=stream)
255+
if GEVENT_CONFIG.print_blocking_reports:
256+
stream = hub.exception_stream
257+
for line in report:
258+
# Printing line by line may interleave with other things,
259+
# but it should also prevent a "reentrant call to print"
260+
# when the report is large.
261+
print(line, file=stream)
261262
return (active_greenlet, report)
262263

263264
def ignore_current_greenlet_blocking(self):

0 commit comments

Comments
 (0)