1 回答

TA貢獻1854條經驗 獲得超8個贊
我認為有兩個問題:
stop_server
并且每個子進程都啟動一個新的子進程,這只能在 中完成。command
start_server
start_server
在子進程完成之前使用哪些塊,從而防止程序在運行時向服務器發送任何其他命令。server.communicate()
相反
start_server
應創建子進程,然后將其存儲在可由 和 訪問的變量中,stop_server
command
server.communicate
應在 中完成。stop_server
stop_server
也只是 的一個特例。command
import subprocess
class Server:
def __init__(self, server_start_bat, dir):
self.server_start_bat = server_start_bat
self.dir = dir
def start_server(self):
self.server = subprocess.Popen(self.server_start_bat, cwd=self.dir, shell=True, stdin=subprocess.PIPE, text=True)
def stop_server(self):
self.command('stop')
self.server.communicate()
def command(self, command):
self.server.stdin.write(f'{command}\n')
添加回答
舉報