我正在运行与此类似的代码:
def listen():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
#conn.sendall(data)
print(data)
def main():
newpid = os.fork()
if newpid == 0:
listen()
else:
pids = (os.getpid(), newpid)
print("parent: %d, child: %d\n" % pids)
sys.exit()
if __name__=="__main__":
main()
当我将其作为服务运行,因此系统状态为..时,我会看到类似这样的内容:
● .service - listen for url input.
Loaded: loaded (/lib/systemd/system/.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-07-15 18:50:35 EDT; 1s ago
Main PID: 11618 (python)
Tasks: 2 (limit: 4915)
CGroup: /system.slice/.service
├─11618 //venv/bin/python /.py
└─11619 //venv/bin/python /.py
很明显,哪个进程是父进程,哪个进程是子进程,但是我需要从子进程等生成子进程,并需要命名它们/从systemctl显示更好的树。这可能吗?