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 5 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()
mitogen.core.set_block(slave_fd)
disable_echo(master_fd)
disable_echo(slave_fd)
try:
mitogen.core.set_block(slave_fd)
disable_echo(master_fd)
disable_echo(slave_fd)
pid = detach_popen(
args=args,
stdin=slave_fd,
@ -467,27 +489,30 @@ 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)
disable_echo(master_fd)
disable_echo(slave_fd)
try:
pid = detach_popen(
args=args,
stdin=childfp,
stdout=childfp,
stderr=slave_fd,
preexec_fn=_acquire_controlling_tty,
close_fds=True,
)
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,
stdout=childfp,
stderr=slave_fd,
preexec_fn=_acquire_controlling_tty,
close_fds=True,
)
except Exception:
parentfp.close()
childfp.close()
raise
except Exception:
os.close(master_fd)
os.close(slave_fd)
parentfp.close()
childfp.close()
raise
os.close(slave_fd)

Loading…
Cancel
Save