Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #33213: use @cached_function for the spawned processes file.
Browse files Browse the repository at this point in the history
This fixes an accidental change in behavior that was introduced in an
earlier commit. The path to this file shouldn't be computed when the
module is loaded -- only when it is requested. A @lazy_string would
work here, but in my opinion the added abstraction on top of the
more-familiar @cached_function is not worth it.
  • Loading branch information
orlitzky authored and dimpase committed Jun 13, 2022
1 parent 78db2fd commit 56e2964
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/sage/interfaces/quit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@

import os

import atexit, tempfile
_spd = tempfile.TemporaryDirectory()
SAGE_SPAWNED_PROCESS_FILE = os.path.join(_spd.name, "spawned_processes")
atexit.register(lambda: _spd.cleanup())
from sage.misc.cachefunc import cached_function


@cached_function
def sage_spawned_process_file():
"""
EXAMPLES::
sage: from sage.interfaces.quit import sage_spawned_process_file
sage: len(sage_spawned_process_file()) > 1
True
"""
import atexit, tempfile
f = tempfile.NamedTemporaryFile(delete=False)
f.close()
atexit.register(lambda: os.remove(f.name))
return f.name


def register_spawned_process(pid, cmd=''):
"""
Expand All @@ -27,7 +42,7 @@ def register_spawned_process(pid, cmd=''):
cmd = cmd.strip().split()[0]
# This is safe, since only this process writes to this file.
try:
with open(SAGE_SPAWNED_PROCESS_FILE, 'a') as o:
with open(sage_spawned_process_file(), 'a') as o:
o.write('%s %s\n'%(pid, cmd))
except IOError:
pass
Expand Down Expand Up @@ -88,9 +103,7 @@ def kill_spawned_jobs(verbose=False):
sage: sage.interfaces.quit.expect_quitall()
"""
if not os.path.exists(SAGE_SPAWNED_PROCESS_FILE):
return
with open(SAGE_SPAWNED_PROCESS_FILE) as f:
with open(sage_spawned_process_file()) as f:
for L in f:
i = L.find(' ')
pid = L[:i].strip()
Expand Down

0 comments on commit 56e2964

Please sign in to comment.