Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/interfaces/cleaner.py
4045 views
1
"""nodoctest
2
"""
3
4
###############################################################################
5
# Sage: System for Algebra and Geometry Experimentation
6
# Copyright (C) 2007 William Stein <[email protected]>
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# The full text of the GPL is available at:
9
# http://www.gnu.org/licenses/
10
###############################################################################
11
# Triva Note: For the name "sage-cleaner", think of the
12
# "The Cleaner" from Pulp Fiction:
13
# http://www.frankjankowski.de/quiz/illus/keitel.jpg
14
###############################################################################
15
import os
16
17
import sage.misc.misc as misc
18
F = '%s/spawned_processes'%misc.SAGE_TMP
19
20
def cleaner(pid, cmd=''):
21
if cmd != '':
22
cmd = cmd.strip().split()[0]
23
# This is safe, since only this process writes to this file.
24
if os.path.exists(F):
25
o = open(F,'a')
26
else:
27
if not os.path.exists(misc.SAGE_TMP):
28
return
29
o = open(F,'w')
30
o.write('%s %s\n'%(pid, cmd))
31
o.close()
32
start_cleaner_if_not_running()
33
34
################
35
36
def start_cleaner_if_not_running():
37
D = '%s/tmp_cleaner.pid'%misc.DOT_SAGE
38
try:
39
pid = int(open(D).read())
40
os.kill(pid,0)
41
return
42
except (IOError, OSError, ValueError):
43
os.system('sage-cleaner >/dev/null 2>/dev/null &') # it has died
44
45
46
47
48