# HG changeset patch
# User William Stein <wstein@gmail.com>
# Date 1277358107 25200
# Node ID 7523af317075c0a7b650c328e28eeb10f2cdcc9a
# Parent  1b1239c4c86ae3026c41d961d2775017ad681d88
trac 8410: Improve robustness of @parallel

diff -r 1b1239c4c86a -r 7523af317075 sage/parallel/use_fork.py
--- a/sage/parallel/use_fork.py	Tue Jun 22 21:00:36 2010 -0700
+++ b/sage/parallel/use_fork.py	Wed Jun 23 22:41:47 2010 -0700
@@ -109,25 +109,29 @@
                                 os.kill(pid,9)
                                 X[-1] = ' (timed out)'
                     else:
-                        # collect data from process that successfully terminated
-                        sobj = os.path.join(dir, '%s.sobj'%pid)
-                        if not os.path.exists(sobj):
-                            X = "NO DATA" + workers[pid][-1]  # the message field
-                        else:
-                            X = load(sobj, compress=False)
-                            os.unlink(sobj)
-                        out = os.path.join(dir, '%s.out'%pid)
-                        if not os.path.exists(out):
-                            output = "NO OUTPUT" 
-                        else:
-                            output = open(out).read()
-                            os.unlink(out)
+                        # If the computation was interrupted the pid
+                        # might not be in the workers list, in which
+                        # case we skip this.
+                        if pid in workers:
+                            # collect data from process that successfully terminated
+                            sobj = os.path.join(dir, '%s.sobj'%pid)
+                            if not os.path.exists(sobj):
+                                X = "NO DATA" + workers[pid][-1]  # the message field
+                            else:
+                                X = load(sobj, compress=False)
+                                os.unlink(sobj)
+                            out = os.path.join(dir, '%s.out'%pid)
+                            if not os.path.exists(out):
+                                output = "NO OUTPUT" 
+                            else:
+                                output = open(out).read()
+                                os.unlink(out)
 
-                        if output.strip():
-                            print output,
+                            if output.strip():
+                                print output,
 
-                        yield (workers[pid][0], X)
-                        del workers[pid]
+                            yield (workers[pid][0], X)
+                            del workers[pid]
 
         except Exception, msg:
             print msg
