Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/interfaces/quit.py
4045 views
1
"""
2
Quitting interfaces
3
"""
4
5
6
################################################################################
7
# Copyright (C) 2010 William Stein <[email protected]>
8
#
9
# Distributed under the terms of (any version of) the GNU
10
# General Public License (GPL). The full text of the GPL is available at:
11
#
12
# http://www.gnu.org/licenses/
13
################################################################################
14
15
import os
16
17
expect_objects = []
18
19
def expect_quitall(verbose=False):
20
"""
21
EXAMPLES::
22
23
sage: sage.interfaces.quit.expect_quitall()
24
sage: gp.eval('a=10')
25
'10'
26
sage: gp('a')
27
10
28
sage: sage.interfaces.quit.expect_quitall()
29
sage: gp('a')
30
a
31
sage: sage.interfaces.quit.expect_quitall(verbose=True)
32
Exiting spawned PARI/GP interpreter process.
33
"""
34
for P in expect_objects:
35
R = P()
36
if not R is None:
37
try:
38
R.quit(verbose=verbose)
39
pass
40
except RuntimeError:
41
pass
42
kill_spawned_jobs()
43
44
def kill_spawned_jobs(verbose=False):
45
"""
46
INPUT:
47
48
- ``verbose`` -- bool (default: False); if True, display a
49
message each time a process is sent a kill signal
50
51
EXAMPLES::
52
53
sage: gp.eval('a=10')
54
'10'
55
sage: sage.interfaces.quit.kill_spawned_jobs(verbose=False)
56
sage: sage.interfaces.quit.expect_quitall()
57
sage: gp.eval('a=10')
58
'10'
59
sage: sage.interfaces.quit.kill_spawned_jobs(verbose=True)
60
Killing spawned job ...
61
62
After doing the above, we do the following to avoid confusion in other doctests::
63
64
sage: sage.interfaces.quit.expect_quitall()
65
"""
66
from sage.misc.misc import SAGE_TMP
67
file = os.path.join(SAGE_TMP, 'spawned_processes')
68
if not os.path.exists(file):
69
return
70
for L in open(file).readlines():
71
i = L.find(' ')
72
pid = L[:i].strip()
73
cmd = L[i+1:].strip()
74
try:
75
if verbose:
76
print "Killing spawned job %s"%pid
77
os.killpg(int(pid), 9)
78
except:
79
pass
80
81
def is_running(pid):
82
"""
83
Return True if and only if there is a process with id pid running.
84
"""
85
try:
86
os.kill(int(pid),0)
87
return True
88
except (OSError, ValueError):
89
return False
90
91
92
def invalidate_all():
93
"""
94
Invalidate all of the expect interfaces.
95
96
This is used, e.g., by the fork-based @parallel decorator.
97
98
EXAMPLES::
99
100
sage: a = maxima(2); b = gp(3)
101
sage: a, b
102
(2, 3)
103
sage: sage.interfaces.quit.invalidate_all()
104
sage: a
105
Traceback (most recent call last):
106
...
107
ValueError: The maxima session in which this object was defined is no longer running.
108
sage: b
109
Traceback (most recent call last):
110
...
111
ValueError: The pari session in which this object was defined is no longer running.
112
113
However the maxima and gp sessions should still work out, though with their state reset:
114
115
sage: a = maxima(2); b = gp(3)
116
sage: a, b
117
(2, 3)
118
"""
119
for I in expect_objects:
120
I1 = I()
121
if I1:
122
I1._expect = None # Invalidate this interface
123
I1._Expect__initialized = False
124
I1._session_number += 1
125
I1.quit()
126
127