zoukankan      html  css  js  c++  java
  • python 子进程 subpocess 的使用方法简单介绍

    python的子进程嘛,就是利用python打开一个子进程(当然像是一句废话),但是可能和我们理解的不太一样。

    一:如何理解?

    我们可能的理解:多开一个进程运行某个python函数(如果只想实现这个功能,请使用multiprocessing包)

    正确的理解:python通过shell/cmd 打开一个新的程序进程,而不限于python函数,比如我们可以开一个“ls”指令的进程列出当前文件夹下的文件,这个“ls”指令明显是一个shell通用函数,而不是python

    函数:

    # 打开子进程运行“ls”。输出当前文件夹下文件
    import subprocess p = subprocess.run(["ls"])

    二. 如何使用?

    当我们想单纯地利用subprocess打开一个进程运行python函数的时候,我们甚至要迂回地去做:

    比方说这样:

    (1)新建一个需要运行的函数脚本 test_print.py

    import sys
    
    def print_it(a, b , c):
        print(a)
        print(b)
        print(c)
    
    
    if __name__ == "__main__":
    
        print_it(sys.argv[1], sys.argv[2], sys.argv[3])

    (2)再建一个脚本,通过传递参数的方式运行 test_print.py

    import subprocess
    
    
    p = subprocess.run(["python", "test_print.py", "a1", "b2", "c3"])
    pp = subprocess.run(["python", "test_print.py", "d4", "e5", "f6"])

    (3) 输出结果:

    a1

    b2

    c3

    d4

    e5

    f6

    三:一些简单用法

    1. 比方说重定向输出:

    (1)依旧是新建一个需要运行的函数脚本 test_print.py

    import sys
    
    def print_it(a, b , c):
        print(a)
        print(b)
        print(c)
    
    
    if __name__ == "__main__":
    
        print_it(sys.argv[1], sys.argv[2], sys.argv[3])

    (2)再建一个脚本,通过传递参数的方式运行 test_print.py

    import subprocess
    
    p = subprocess.Popen(["python", "test_print.py", "a1", "b2", "c3"], stdout=subprocess.PIPE, shell=True) #shell=True 为必须,否则stdout无法读出
    pp = subprocess.Popen(["python", "test_print.py", "d4", "e5", "f6"], stdout=subprocess.PIPE, shell=True) 
    
    print(p.stdout.read()) 
    print(pp.stdout.read())

    然而此时,输出的结果是二进制文件

    b'a1 b2 c3 '
    b'd4 e5 f6 '

    我们需要对此进行处理(当然你不处理也可以,就是看着别扭)

    import subprocess
    
    p = subprocess.Popen(["python", "test_print.py", "a1", "b2", "c3"], stdout=subprocess.PIPE, shell=True) #shell=True 为必须,否则stdout无法读出
    pp = subprocess.Popen(["python", "test_print.py", "d4", "e5", "f6"], stdout=subprocess.PIPE, shell=True) 
    
    # 用str转化一下就好。
    print(str(p.stdout.read(), encoding = "utf8")) print(str(pp.stdout.read(), encoding = "utf8"))

    (3)定向到外部文件

    import subprocess
    
    # 注意,此步骤为必须 f_handler=open('out.log', 'w')
    p
    = subprocess.run(["python", "test_print.py", "a1", "b2", "c3"], stdout=f_handler) pp = subprocess.run(["python", "test_print.py", "d4", "e5", "f6"], stdout=f_handler)

    # 一个错误用法
    p_error = subprocess.run(["python", "test_print.py", "d4", "e5", "f6"], stdout='out.log') # 这样是不行的

    我们会发现,屏幕上什么都不会显示,输出结果已经导入到out.log里面了

    。。。。未完待续

  • 相关阅读:
    Linux查看CPU信息
    sed总结
    angularjs学习笔记--service、$http、$scope、angularjs指令、事件、api
    angularjs学习笔记--ng-model、controller、DI、$scope、$provide
    angularjs学习笔记--$http、数据渲染到表格、路由、依赖注入、provider
    angularjs学习笔记--主html&template html&module&template js、模块、控制器、双向数据绑定、过滤器
    angularjs学习笔记--视图、模板、组件、$http、$q、module
    angular学习笔记---下载并运行nicefish项目
    angular学习笔记---通过angular/cli建一个新的项目
    json模拟数据交互
  • 原文地址:https://www.cnblogs.com/chenyansu/p/10772085.html
Copyright © 2011-2022 走看看