Skip to content

Commit

Permalink
Fix dsage doctest failures caused by not joining the thread before ex…
Browse files Browse the repository at this point in the history
…iting and

not explicitely deleting subprocess.Popen references.
  • Loading branch information
yqiang committed Mar 30, 2008
1 parent 11cbe85 commit 44373ca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/sage/dsage/dsage.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def spawn(cmd, verbose=True, stdout=None, stdin=None):
if verbose:
print 'Spawned %s (pid = %s)\n' % (' '.join(cmdl), process.pid)

return process.pid
return process


class DistributedSage(object):
Expand Down Expand Up @@ -174,17 +174,19 @@ def kill_all(self):
self.kill_worker()
self.kill_server()


def kill_worker(self):
try:
os.kill(self.worker_pid, 9)
os.kill(self.worker_proc.pid, 9)
self.worker_proc.wait()
del self.worker_proc
except OSError, msg:
print 'Error killing worker: %s' % msg


def kill_server(self):
try:
os.kill(self.server_pid, 9)
os.kill(self.server_proc.pid, 9)
self.server_proc.wait()
del self.server_proc
except OSError, msg:
print 'Error killing server: %s' % msg

Expand Down Expand Up @@ -248,7 +250,7 @@ def write_tac(tac):
except:
pass
cmd += '--logfile=%s -y dsage_server.tac' % (log_file)
server_pid = spawn(cmd, verbose=verbose)
self.server_proc = spawn(cmd, verbose=verbose)
# Need the following hack since subprocess.Popen reports the wrong
# pid when launching an application with twistd
while True:
Expand Down Expand Up @@ -288,7 +290,7 @@ def worker(self, server='localhost', port=8081, workers=2, poll=1.0,
if not blocking:
cmd += ' --noblock'
cmd = 'python ' + SAGE_ROOT + '/local/bin/' + cmd
self.worker_pid = spawn(cmd, verbose=verbose)
self.worker_proc = spawn(cmd, verbose=verbose)
else:
cmd = 'python ' + SAGE_ROOT + '/local/bin/' + cmd
os.system(cmd)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/dsage/interface/dsage_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,4 +868,4 @@ def handler(signum, frame):
while self.status != 'completed':
time.sleep(1.0)
self.get_job()
signal.alarm(0)
signal.alarm(0)
8 changes: 8 additions & 0 deletions src/sage/dsage/tests/testdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@
True
sage: t.result # long time
15
The following code block makes sure that things exit cleanly
sage: dsage.kill_all()
sage: from twisted.internet import reactor
sage: reactor.callFromThread(reactor.stop)
sage: d._dsage_thread.join()
[DSage] Closed connection to localhost
"""

0 comments on commit 44373ca

Please sign in to comment.