@@ -41,6 +41,7 @@ class which means it will use OMCSessionZMQ by default. If you want to use
4141import logging
4242import os
4343import platform
44+ import signal
4445import subprocess
4546import sys
4647import tempfile
@@ -120,11 +121,28 @@ def __init__(self, readonly=False):
120121 self ._omc_log_file = None
121122
122123 def __del__ (self ):
123- self .sendExpression ("quit()" )
124+ try :
125+ self .sendExpression ("quit()" )
126+ except :
127+ pass
124128 self ._omc_log_file .close ()
129+ if sys .version_info .major >= 3 :
130+ self ._omc_process .wait (timeout = 1.0 )
131+ else :
132+ for i in range (0 ,100 ):
133+ time .sleep (0.01 )
134+ if self ._omc_process .poll () is not None :
135+ break
125136 # kill self._omc_process process if it is still running/exists
126137 if self ._omc_process .returncode is None :
127- self ._omc_process .kill ()
138+ print ("OMC did not exit after being sent the quit() command; killing the process with pid=%s" % str (self ._omc_process .pid ))
139+ if sys .platform == "win32" :
140+ self ._omc_process .kill ()
141+ self ._omc_process .wait ()
142+ else :
143+ os .killpg (os .getpgid (self ._omc_process .pid ), signal .SIGTERM )
144+ self ._omc_process .kill ()
145+ self ._omc_process .wait ()
128146
129147 def _create_omc_log_file (self , suffix ):
130148 if sys .platform == 'win32' :
@@ -143,7 +161,8 @@ def _start_omc_process(self):
143161 my_env ["PATH" ] = omhome_bin + os .pathsep + my_env ["PATH" ]
144162 self ._omc_process = subprocess .Popen (self ._omc_command , shell = True , stdout = self ._omc_log_file , stderr = self ._omc_log_file , env = my_env )
145163 else :
146- self ._omc_process = subprocess .Popen (self ._omc_command , shell = True , stdout = self ._omc_log_file , stderr = self ._omc_log_file )
164+ # Because we spawned a shell, and we need to be able to kill OMC, create a new process group for this
165+ self ._omc_process = subprocess .Popen (self ._omc_command , shell = True , stdout = self ._omc_log_file , stderr = self ._omc_log_file , preexec_fn = os .setsid )
147166 return self ._omc_process
148167
149168 def _set_omc_command (self , omc_path , args ):
@@ -513,24 +532,21 @@ def _connect_to_omc(self, timeout):
513532 self ._port_file = os .path .join (self ._temp_dir , self ._port_file ).replace ("\\ " , "/" )
514533 self ._omc_zeromq_uri = "file:///" + self ._port_file
515534 # See if the omc server is running
516- if os .path .isfile (self ._port_file ):
517- logger .info ("OMC Server is up and running at {0}" .format (self ._omc_zeromq_uri ))
518- else :
519- attempts = 0
520- while True :
521- if not os .path .isfile (self ._port_file ):
522- time .sleep (timeout )
523- attempts += 1
524- if attempts == 10 :
525- name = self ._omc_log_file .name
526- self ._omc_log_file .close ()
527- logger .error ("OMC Server is down. Please start it! Log-file says:\n %s" % open (name ).read ())
528- raise Exception
529- else :
530- continue
535+ attempts = 0
536+ while True :
537+ if not os .path .isfile (self ._port_file ):
538+ time .sleep (timeout )
539+ attempts += 1
540+ if attempts == 10 :
541+ name = self ._omc_log_file .name
542+ self ._omc_log_file .close ()
543+ logger .error ("OMC Server is down. Please start it! Log-file says:\n %s" % open (name ).read ())
544+ raise Exception ("OMC Server is down. Could not open file %s" % self ._port_file )
531545 else :
532- logger .info ("OMC Server is up and running at {0}" .format (self ._omc_zeromq_uri ))
533- break
546+ continue
547+ else :
548+ logger .info ("OMC Server is up and running at {0} pid={1}" .format (self ._omc_zeromq_uri , self ._omc_process .pid ))
549+ break
534550
535551 # Read the port file
536552 with open (self ._port_file , 'r' ) as f_p :
0 commit comments