parent: synchronize get_core_source()

Single task 100 SSH target run, before:

        3533181 function calls (3533083 primitive calls) in 616.688 seconds
        User time (seconds): 32.52
        System time (seconds): 2.71
        Percent of CPU this job got: 64%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:54.88

After:

        451602 function calls (451504 primitive calls) in 570.746 seconds
        User time (seconds): 29.48
        System time (seconds): 2.81
        Percent of CPU this job got: 67%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:48.20
pull/564/head
David Wilson 6 years ago
parent 2399a9e621
commit 7ca927608c

@ -155,7 +155,11 @@ def get_sys_executable():
return '/usr/bin/python'
def get_core_source():
_core_source_cache = None
_core_source_lock = threading.Lock()
def _get_core_source():
"""
In non-masters, simply fetch the cached mitogen.core source code via the
import mechanism. In masters, this function is replaced with a version that
@ -164,6 +168,24 @@ def get_core_source():
return inspect.getsource(mitogen.core)
def get_core_source():
"""
_get_core_source() is expensive, even with @lru_cache in minify.py, threads
can enter it simultaneously causing severe slowdowns.
"""
global _core_source_cache
if _core_source_cache is not None:
return _core_source_cache
_core_source_lock.acquire()
try:
if _core_source_cache is None:
_core_source_cache = _get_core_source()
return _core_source_cache
finally:
_core_source_lock.release()
def get_default_remote_name():
"""
Return the default name appearing in argv[0] of remote machines.
@ -430,11 +452,11 @@ def tty_create_child(args):
`(pid, tty_fd, None)`
"""
master_fd, slave_fd = openpty()
try:
mitogen.core.set_block(slave_fd)
disable_echo(master_fd)
disable_echo(slave_fd)
try:
pid = detach_popen(
args=args,
stdin=slave_fd,
@ -467,14 +489,15 @@ def hybrid_tty_create_child(args):
`(pid, socketpair_fd, tty_fd)`
"""
master_fd, slave_fd = openpty()
parentfp, childfp = create_socketpair()
mitogen.core.set_block(slave_fd)
mitogen.core.set_block(childfp)
try:
disable_echo(master_fd)
disable_echo(slave_fd)
mitogen.core.set_block(slave_fd)
parentfp, childfp = create_socketpair()
try:
mitogen.core.set_block(childfp)
pid = detach_popen(
args=args,
stdin=childfp,
@ -484,11 +507,13 @@ def hybrid_tty_create_child(args):
close_fds=True,
)
except Exception:
os.close(master_fd)
os.close(slave_fd)
parentfp.close()
childfp.close()
raise
except Exception:
os.close(master_fd)
os.close(slave_fd)
raise
os.close(slave_fd)
childfp.close()

Loading…
Cancel
Save