Question

I want to start a subprocess by function subprocess.call(), and i want the main loop waits until the subprocess has started.

I thought to use a threading.Lock:

  • acquiring it before the the subprocess.call()
  • then trying to re-acquire it (so it waits)
  • and releasing it after subprocess.call() (main loop is unlocked)

The subprocess has to run as a daemon, so i put the subprocess.call() inside a thread.

But it stops after the subprocess.call(), i think it's just waiting because pyro-ns is a daemon that listens on a port. How can i do this working?

See the code, it's easier:

self.__wait_pyroNS_lock=threading.Lock()
self.__logger.debug('i get wait_pyroNS_lock')
self.__wait_pyroNS_lock.acquire()

pyro_ns = threading.Thread(name='Pyro Name Server', target=self.__Pyro_NameServer, args=(self.__wait_pyroNS_lock,))
pyro_ns.setDaemon(True)

self.__logger.debug('starting thread def self.__Pyro_NameServer')
pyro_ns.start()

self.__wait_pyroNS_lock.acquire() # <--- MAIN LOOP WAITING HERE
self.__wait_pyroNS_lock.release()



def __Pyro_NameServer(self, wait_pyroNS_lock):

    try:
        self.__logger.debug('def __Pyro_NameServer')

        self.__logger.debug('starting pyro-ns')
        retcode = subprocess.call("pyro-ns", shell=True) # <--- THREAD STOPS HERE, it doesn't return, so i don't get any code and it's all locked.

        if retcode != 0:
            self.__logger.debug('command pyro-ns: fail, error code %d' % retcode)
            else:
                self.__logger.debug('pyro-ns has started')

            self.__logger.debug('releasing wait_pyroNS_lock')
            wait_pyroNS_lock.release() # <--- IT NEVER RUNS, it releases the lock, the main loop is unlocked

        except Exception as e:
            self.__logger.error('%s' % str(e))
Was it helpful?

Solution

I've decided to use the API provided by PyRO.

pyro_ns_object = Pyro.naming.NameServerStarter()
pyro_ns = threading.Thread(name='Pyro Name Server', target=self.__Pyro_NameServer, args=(pyro_ns_object,))
pyro_ns.setDaemon(True)
self.__logger.debug('start thread def self.__Pyro_NameServer')
pyro_ns.start()

while True:
    if pyro_ns_object.waitUntilStarted(timeout=0.1):
        break
    else:
        self.__logger.debug('waiting pyro-ns')


def __Pyro_NameServer(self, pyro_ns_object):

    try:
        self.__logger.debug('def __Pyro_NameServer')

        self.__logger.debug('starting pyro-ns')
        pyro_ns_object.start()

    except Exception as e:
        self.__logger.error('%s' % str(e))
        raise e
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top