zoukankan      html  css  js  c++  java
  • python 子进程管道传递

    netstat   ano |findstr 8080  ,python subprocess 管道使用:

    import subprocess
    import threading
    def demo(iter_list, if_child=True):
        if not if_child:
            sub = subprocess.Popen(iter_list, shell=True, stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
            stdout, stderr = sub.communicate()
            if sub.returncode == 0:
                print("正常输出为:", stdout)
                print("code 为:", sub.returncode)
            else:
                print("stderr is :", stderr)
        else:
            p1 = subprocess.Popen(iter_list[0], shell=False,
                                  stdout=subprocess.PIPE, universal_newlines=True, stderr=subprocess.PIPE)
    
            p2 = subprocess.Popen(iter_list[1], shell=False, stdin=p1.stdout,
                                  stdout=subprocess.PIPE, universal_newlines=True)
            stdout, stderr = p2.communicate()
            if p2.returncode == 0:
                print("正常输出为:", stdout)
                print("code 为:", p2.returncode)
            else:
                print("stderr is :", stderr)
    if __name__ == '__main__':
        arg_list = [[["netstat", "-ano"], ["findstr", "8080"]] for i in range(2)]
        # a=["netstat -ano|findstr 8080" for m in range(4)]
        pool = [threading.Thread(target=demo, args=(i,)) for i in arg_list]
        for th in pool:
            th.start()
        for t in pool:
            t.join()
    

      

  • 相关阅读:
    C++ *和&
    leetcode 338. Bit位计数
    leetcode 216. 组合总和 III
    leetcode 40. 组合总和 II
    c++ vector 常用函数
    leetcode 491. 递增子序列
    leetcode 441. 排列硬币
    leetcode 258. 各位相加
    leetcode 415. 字符串相加
    leetcode 67. 二进制求和
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/12469998.html
Copyright © 2011-2022 走看看