Skip to content

Commit 55e716b

Browse files
committed
Fix subprocess bugs
修复子进程运行结束前可以被新启动的同类子进程打断的问题; 修复在非默认utf-8平台下QByteArray解码字符串错误的问题;
1 parent 70f2998 commit 55e716b

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/py2exe-gui/Core/packaging.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, parent: Optional[QObject] = None):
2727

2828
def get_pyinstaller_args(self, arg: tuple[str, str]) -> None:
2929
"""
30-
解析传递来的PyInstaller运行参数,并以正确的顺序添加至命令参数字典 \n
30+
解析传递来的PyInstaller运行参数,并添加至命令参数字典 \n
3131
:param arg: 运行参数
3232
"""
3333

@@ -38,7 +38,8 @@ def get_pyinstaller_args(self, arg: tuple[str, str]) -> None:
3838

3939
def set_pyinstaller_args(self) -> None:
4040
"""
41-
将命令参数字典中的参数按顺序添加到命令参数列表中
41+
将命令参数字典中的参数按顺序添加到命令参数列表中 \n
42+
:return: None
4243
"""
4344

4445
self._args = [] # 避免重复添加
@@ -66,6 +67,7 @@ def run_packaging_process(self) -> None:
6667
:return: None
6768
"""
6869

69-
self.subprocess = QSubProcessTool()
70+
if self.subprocess is None: # 确保只在首次调用时实例化一个QSubProcess对象
71+
self.subprocess = QSubProcessTool()
7072
self.subprocess.output.connect(lambda val: print(val)) # 测试用
7173
self.subprocess.start_process("pyinstaller", self._args)

src/py2exe-gui/Core/subprocess_tool.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from sys import getdefaultencoding
12
from typing import Optional, Sequence
23

34
from PySide6 import QtCore
@@ -21,7 +22,7 @@ def __init__(self, parent: Optional[QtCore.QObject] = None) -> None:
2122

2223
def start_process(self, program: str, arguments: Sequence[str]) -> None:
2324
"""
24-
启动子进程 \n
25+
使用给定参数启动指定子进程 \n
2526
:param program: 子进程命令
2627
:param arguments: 子进程参数
2728
:return: None
@@ -30,18 +31,27 @@ def start_process(self, program: str, arguments: Sequence[str]) -> None:
3031
if self.process is None: # 防止在子进程运行结束前重复启动
3132
self.process = QtCore.QProcess()
3233

34+
self.process.stateChanged.connect(self._handle_state) # type: ignore
3335
self.process.readyReadStandardOutput.connect(self._handle_stdout) # type: ignore
3436
self.process.readyReadStandardError.connect(self._handle_stderr) # type: ignore
35-
self.process.stateChanged.connect(self._handle_state) # type: ignore
37+
self.process.started.connect(self._process_started) # type: ignore
3638
self.process.finished.connect(self._process_finished) # type: ignore
3739

3840
self.process.start(program, arguments)
3941

42+
def _process_started(self) -> None:
43+
"""
44+
处理子进程的槽 \n
45+
:return: None
46+
"""
47+
pass
48+
4049
def _process_finished(self) -> None:
4150
"""
4251
处理子进程的槽 \n
4352
:return: None
4453
"""
54+
4555
self.output.emit((self.FINISHED, "Subprocess finished."))
4656
self.process = None
4757

@@ -53,7 +63,7 @@ def _handle_stdout(self) -> None:
5363

5464
if self.process:
5565
data = self.process.readAllStandardOutput()
56-
stdout = bytes(data).decode("utf8")
66+
stdout = bytes(data).decode(getdefaultencoding())
5767
self.output.emit((self.STDOUT, stdout))
5868

5969
def _handle_stderr(self) -> None:
@@ -64,7 +74,7 @@ def _handle_stderr(self) -> None:
6474

6575
if self.process:
6676
data = self.process.readAllStandardError()
67-
stderr = bytes(data).decode("utf8")
77+
stderr = bytes(data).decode(getdefaultencoding())
6878
self.output.emit((self.STDERR, stderr))
6979

7080
def _handle_state(self, state: QtCore.QProcess.ProcessState) -> None:

0 commit comments

Comments
 (0)