子进程调用 'pyro-ns' 与线程同步

2024-06-25 22:47:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想按函数启动子流程subprocess.call(),我希望主循环等待子进程启动。在

我想用穿线。锁定:

  • 在subprocess.call()
  • 然后,尝试重新获取它(因此它等待)
  • 然后释放它subprocess.call()(主循环未锁定)

子进程必须作为守护进程运行,因此我将subprocess.call()在线程中。在

但它在subprocess.call(),我认为它只是在等待,因为pyro ns是一个侦听端口的守护程序。我怎样才能做到这一点?在

查看代码,这样更简单:

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))

Tags: debugselflock进程defcallloggersubprocess
1条回答
网友
1楼 · 发布于 2024-06-25 22:47:36

我决定使用PyRO提供的API。在

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

相关问题 更多 >