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.
issue510
David Wilson 6 years ago
parent cef7c1ccc7
commit dd30a907ce

@ -93,27 +93,6 @@ def getenv_int(key, default=0):
return default 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): class MuxProcess(object):
""" """
Implement a subprocess forked from the Ansible top-level, as a safe place 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: if faulthandler is not None:
faulthandler.enable() faulthandler.enable()
setup_gil() mitogen.utils.setup_gil()
cls.unix_listener_path = mitogen.unix.make_socket_path() cls.unix_listener_path = mitogen.unix.make_socket_path()
cls.worker_sock, cls.child_sock = socket.socketpair() cls.worker_sock, cls.child_sock = socket.socketpair()
atexit.register(lambda: clean_shutdown(cls.worker_sock)) atexit.register(lambda: clean_shutdown(cls.worker_sock))

@ -617,6 +617,7 @@ A random assortment of utility functions useful on masters and children.
.. currentmodule:: mitogen.utils .. currentmodule:: mitogen.utils
.. autofunction:: setup_gil
.. autofunction:: disable_site_packages .. autofunction:: disable_site_packages
.. autofunction:: log_to_file .. autofunction:: log_to_file
.. autofunction:: run_with_router(func, \*args, \**kwargs) .. autofunction:: run_with_router(func, \*args, \**kwargs)

@ -45,6 +45,27 @@ else:
iteritems = dict.iteritems 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(): def disable_site_packages():
""" """
Remove all entries mentioning ``site-packages`` or ``Extras`` from Remove all entries mentioning ``site-packages`` or ``Extras`` from

@ -6,9 +6,9 @@ import threading
import time import time
import mitogen import mitogen
import mitogen.utils
import ansible_mitogen.process mitogen.utils.setup_gil()
ansible_mitogen.process.setup_gil()
X = 20000 X = 20000

@ -2,11 +2,12 @@
Measure latency of local RPC. Measure latency of local RPC.
""" """
import mitogen
import time import time
import ansible_mitogen.process import mitogen
ansible_mitogen.process.setup_gil() import mitogen.utils
mitogen.utils.setup_gil()
try: try:
xrange xrange

Loading…
Cancel
Save