From dd30a907ceacc9dcfd8dd2fa19f5181dcbe2621e Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 23 Jan 2019 12:44:08 +0000 Subject: [PATCH] issue #477: promote setup_gil() to mitogen.utils This is since ansible_mitogen/process.py is 2.6-only, and I want to use setup_gil() in 2.4 code. --- ansible_mitogen/process.py | 23 +---------------------- docs/api.rst | 1 + mitogen/utils.py | 21 +++++++++++++++++++++ tests/bench/latch_roundtrip.py | 4 ++-- tests/bench/roundtrip.py | 7 ++++--- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/ansible_mitogen/process.py b/ansible_mitogen/process.py index 963867db..22702470 100644 --- a/ansible_mitogen/process.py +++ b/ansible_mitogen/process.py @@ -93,27 +93,6 @@ def getenv_int(key, default=0): return default -def setup_gil(): - """ - Set extremely long GIL release interval to let threads naturally progress - through CPU-heavy sequences without forcing the wake of another thread that - may contend trying to run the same CPU-heavy code. For the new-style work, - this drops runtime ~33% and involuntary context switches by >80%, - essentially making threads cooperatively scheduled. - """ - try: - # Python 2. - sys.setcheckinterval(100000) - except AttributeError: - pass - - try: - # Python 3. - sys.setswitchinterval(10) - except AttributeError: - pass - - class MuxProcess(object): """ Implement a subprocess forked from the Ansible top-level, as a safe place @@ -177,7 +156,7 @@ class MuxProcess(object): if faulthandler is not None: faulthandler.enable() - setup_gil() + mitogen.utils.setup_gil() cls.unix_listener_path = mitogen.unix.make_socket_path() cls.worker_sock, cls.child_sock = socket.socketpair() atexit.register(lambda: clean_shutdown(cls.worker_sock)) diff --git a/docs/api.rst b/docs/api.rst index b1fa215c..3fd70bea 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -617,6 +617,7 @@ A random assortment of utility functions useful on masters and children. .. currentmodule:: mitogen.utils +.. autofunction:: setup_gil .. autofunction:: disable_site_packages .. autofunction:: log_to_file .. autofunction:: run_with_router(func, \*args, \**kwargs) diff --git a/mitogen/utils.py b/mitogen/utils.py index e2820333..07559d56 100644 --- a/mitogen/utils.py +++ b/mitogen/utils.py @@ -45,6 +45,27 @@ else: iteritems = dict.iteritems +def setup_gil(): + """ + Set extremely long GIL release interval to let threads naturally progress + through CPU-heavy sequences without forcing the wake of another thread that + may contend trying to run the same CPU-heavy code. For the new-style + Ansible work, this drops runtime ~33% and involuntary context switches by + >80%, essentially making threads cooperatively scheduled. + """ + try: + # Python 2. + sys.setcheckinterval(100000) + except AttributeError: + pass + + try: + # Python 3. + sys.setswitchinterval(10) + except AttributeError: + pass + + def disable_site_packages(): """ Remove all entries mentioning ``site-packages`` or ``Extras`` from diff --git a/tests/bench/latch_roundtrip.py b/tests/bench/latch_roundtrip.py index 8c779a31..e248c803 100644 --- a/tests/bench/latch_roundtrip.py +++ b/tests/bench/latch_roundtrip.py @@ -6,9 +6,9 @@ import threading import time import mitogen +import mitogen.utils -import ansible_mitogen.process -ansible_mitogen.process.setup_gil() +mitogen.utils.setup_gil() X = 20000 diff --git a/tests/bench/roundtrip.py b/tests/bench/roundtrip.py index 33b3c5b8..fb8f5184 100644 --- a/tests/bench/roundtrip.py +++ b/tests/bench/roundtrip.py @@ -2,11 +2,12 @@ Measure latency of local RPC. """ -import mitogen import time -import ansible_mitogen.process -ansible_mitogen.process.setup_gil() +import mitogen +import mitogen.utils + +mitogen.utils.setup_gil() try: xrange