class test(object):
def __init__(self) -> None:
self.p = None
self.data = None
def request(self):
self.p = subprocess.Popen(['pwsh' ,'-Command' ,'ping', '127.0.0.1'], stdout=subprocess.PIPE, encoding='gbk')
def process(self):
self.data = self.p.communicate()[0]
if __name__ == "__main__":
l = [test()] * 5
for i in l:
i.request()
for i in l:
i.process()
- 为了能够并行(应该是并行吧),所以把 Popen 构造函数和 communicate()放在两个类方法里
- 这样似乎导致了 communicate 的时候子进程已经终止,PIPE 关闭了( I/O operation on closed file.)读不到 stdout
- 有什么办法可以弥补么?或者应该换一种写法?
- 大概有 500 个 test 对象