简体   繁体   中英

Can't terminate child process

I have this code:

class Server(object):
    servers = []

    def __init__(self, name, host, port, process):
        self.name = name.lower()
        self.host, self.port = host, port
        self.process = process
        self.pid = self.process.pid

    @staticmethod
    def register(server):
        Server.servers.append(server)

    @staticmethod
    def unregister(server):
         Server.servers.remove(server)

def start_waitress(host='0.0.0.0', port=8080):
     args = ['--port={}'.format(port),
         '--host={}'.format(host), 'yarr.wsgi:application']

    subprocess.call(['waitress-serve'] + args)

 ...

 host, port = '0.0.0.0', 8080
 process = multiprocessing.Process(target=start_waitress)
 process.start()
 Server.register(Server('waitress', host, port, process))

 ...

for server in Server.servers:
   if server.name == 'waitress':
       server.process.terminate() # Here's the problem
       Server.unregister(server)
       break

The problem is that I can't terminate process after starting it.

The only way I can terminate process is by running kill <pid> .

How do I terminate that process?

As JF Sebastian said, waitress-serve daemonizes itself.

I couldn't fix that with multiprocessing but I found a solution with subprocess .

Instead of creating multiprocessing.Process I'm calling subprocess.Popen(cmd) .

class Server(object):
    servers = []

    def __init__(self, name, host, port, pipe):
        self.name = name.lower()
        self.host, self.port = host, port
        self.pipe = pipe

    @staticmethod
    def register(server):
        Server.servers.append(server)

    @staticmethod
    def unregister(server):
        Server.servers.remove(server)

def start_waitress(host='0.0.0.0', port=8080):
    args = ['--port={}'.format(port),
         '--host={}'.format(host), 'yarr.wsgi:application']

    return subprocess.Popen(['waitress-serve'] + args)

...

host, port = '0.0.0.0', 8080
Server.register(Server('waitress', host, port, start_waitress()))

...

for server in Server.servers:
  if server.name == 'waitress':
      server.pipe.terminate()
      server.pipe.wait()
      Server.unregister(server)
      break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM