@@ -96,65 +96,50 @@ def external_exec(
96
96
:param __no_wait: ⚠️是否不等待, 这意味着需要手动获取返回值和输出
97
97
:return: status code, output | __no_wait 为 True 时返回进程对象
98
98
"""
99
- from rich .markdown import Markdown
99
+ if __expose :
100
+ from rich .markdown import Markdown
100
101
from subprocess import Popen , PIPE
101
- from concurrent .futures import ThreadPoolExecutor , wait
102
-
103
- class MixContent :
104
- def __init__ (self ):
105
- self .content = ""
106
-
107
- def __add__ (self , other ):
108
- self .content += other
109
- return self
110
-
111
- def __str__ (self ):
112
- return self .content
113
-
114
- content = MixContent ()
115
-
116
- def _output (pipe_name : str , process : Popen , content : MixContent ):
117
- ignore_status = (
118
- without_stdout if pipe_name == "stdout" else without_stderr
119
- ) or without_output
120
- for line in iter (eval (f"process.{ pipe_name } .readline" ), "" ):
121
- if not __expose and (
122
- line .startswith ("__START__" )
123
- or line .startswith ("__STOP__" )
124
- or line .startswith ("__SPLIT__" )
125
- ):
102
+ import concurrent .futures
103
+
104
+ def _output (pipe , content , ignore_status ):
105
+ for line in iter (pipe .readline , "" ):
106
+ if not __expose and line .startswith ((
107
+ "__START__" , "__STOP__" ,
108
+ "__TITLE__" , "__RULE__" )):
126
109
continue
127
- content += line
110
+ content . append ( line )
128
111
if ignore_status :
129
112
continue
130
113
line = line .strip ()
131
114
if line .startswith ("__START__" ):
132
- QproDefaultStatus (line .replace ("__START__" , "" )).start ()
115
+ QproDefaultStatus (line .replace ("__START__" , "" ). strip () ).start ()
133
116
elif line .startswith ("__STOP__" ):
134
117
QproDefaultStatus .stop ()
135
- elif line .startswith ("__SPLIT__ " ):
136
- QproDefaultConsole .print (
137
- Markdown ( "# " + line .replace ( "__SPLIT__" , "" ). strip ())
138
- )
118
+ elif line .startswith ("__TITLE__ " ):
119
+ QproDefaultConsole .print (Markdown ( "# " + line . replace ( "__TITLE__" , "" ). strip ()))
120
+ elif line .startswith ( "__RULE__" ):
121
+ QproDefaultConsole . rule ( line . replace ( "__RULE__" , "" ). strip () )
139
122
else :
140
123
QproDefaultConsole .print (line )
141
124
142
- pool = ThreadPoolExecutor ( 2 )
143
- p = Popen (cmd , shell = True , stdout = PIPE , stderr = PIPE , bufsize = 1 , encoding = "utf-8" )
125
+ output_content = []
126
+ process = Popen (cmd , shell = True , stdout = PIPE , stderr = PIPE , bufsize = 1 , encoding = "utf-8" )
144
127
145
- if __no_wait :
146
- return p
128
+ if __no_wait or without_output :
129
+ return process
147
130
148
- wait (
149
- [
150
- pool .submit (_output , "stdout" , p , content ),
151
- pool .submit (_output , "stderr" , p , content ),
152
- ]
153
- )
154
- pool .shutdown ()
155
- ret_code = p .wait ()
131
+ with concurrent .futures .ThreadPoolExecutor (2 ) as executor :
132
+ futures = []
133
+ if not without_stdout :
134
+ futures .append (executor .submit (_output , process .stdout , output_content , without_output ))
135
+ if not without_stderr :
136
+ futures .append (executor .submit (_output , process .stderr , output_content , without_output ))
137
+ concurrent .futures .wait (futures )
156
138
157
- return ret_code , str (content )
139
+ ret_code = process .wait ()
140
+ process .stdout .close ()
141
+ process .stderr .close ()
142
+ return ret_code , '' .join (output_content )
158
143
159
144
160
145
def requirePackage (
0 commit comments