From 24dd7e4d7144605483de2ce5e09f381875485b6b Mon Sep 17 00:00:00 2001
From: "Christoph.Knote" <christoph.knote@physik.uni-muenchen.de>
Date: Thu, 14 Dec 2017 10:46:43 +0100
Subject: [PATCH] More crazy shit.

---
 boxmox/experiment.py | 47 ++++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 13 deletions(-)

diff --git a/boxmox/experiment.py b/boxmox/experiment.py
index 8d65d59..0763b4d 100644
--- a/boxmox/experiment.py
+++ b/boxmox/experiment.py
@@ -4,6 +4,7 @@ if os.name == 'posix' and sys.version_info[0] < 3:
     import subprocess32 as s
 else:
     import subprocess as s
+import threading
 import shutil
 import fnmatch
 import numpy as np
@@ -186,6 +187,27 @@ class Experiment:
 
         return mech
 
+    @property
+    def running(self):
+        '''
+        Check if simulation is running (if started with asynchronous=True).
+        '''
+        if self.pid is None:
+            return False
+        else:
+            return self.pid.poll() is None
+
+    def _run(self, dumbOutput=False):
+        try:
+            self.pid = s.Popen("./" + self.mechanism + ".exe", stdout=s.PIPE, bufsize=1)
+            self.pid.wait()
+            self._pp_run(dumbOutput=dumbOutput)
+        except Exception as e:
+            failDir = self.path + "_failed_at_{:s}".format(datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S"))
+            self.archive( failDir )
+            raise Exception('BOXMOX integration failed: {:s}'.format(str(e)))
+        return
+
     def run(self, dumbOutput=False, asynchronous=False):
         '''
         Run BOXMOX experiment
@@ -202,29 +224,28 @@ class Experiment:
         pwd = os.getcwd()
         os.chdir(self.path)
 
-        try:
-            self.pid = s.Popen("./" + self.mechanism + ".exe", stdout=s.PIPE, bufsize=1)
-            if asynchronous:
-                return
-            self.pid.wait() # wait for the subprocess to exit
+        if asynchronous:
+            thread = threading.Thread(target=self._run, args=(dumbOutput,))
+            thread.start()
+            os.chdir(pwd)
+            return
+        else:
+            self._run(dumbOutput=dumbOutput)
+            os.chdir(pwd)
 
+    def _pp_run(self, dumbOutput=False):
+        try:
             self.lastLog = []
             for line in iter(self.pid.stdout.readline, b''):
                 self.lastLog.append(line.replace('\n',''))
-
-        except Exception as e:
-            os.chdir(pwd)
-            failDir = self.path + "_failed_at_{:s}".format(datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S"))
-            self.archive( failDir )
-            raise Exception('BOXMOX integration failed: {:s}'.format(str(e)))
+        except:
+            pass
 
         if dumbOutput:
             self._populateDumbOutput()
         else:
             self._populateOutput()
 
-        os.chdir(pwd)
-
     outputTypes = {  'Concentrations': { 'ending': '.conc',     'class': ConcentrationOutput },
                      'Rates':          { 'ending': '.rates',    'class': RatesOutput },
                      'Jacobian':       { 'ending': '.jacobian', 'class': JacobianOutput },
-- 
GitLab