zoukankan      html  css  js  c++  java
  • 【Python】 Subprocess module

    1. subprocess.check_output()

    2.subprocess.call()

    3. subprocess.check_call()

    the methods 1.2.3 are are wrapper of subprocess.Popen()

    • example 1:

    Open Visual Studio through python

    This way is incorrect.

    >>> retcode = subprocess.call('devenv.exe', shell=True)
    'devenv.exe' is not recognized as an internal or external command,
    operable program or batch file.

    Another way:

    retcode = subprocess.call("C:Program Files (x86)Microsoft Visual Studio 12.0Common7IDEdevenv.exe",shell=True)
    

      It's done.

    Your VS is opened. But the invoke process is blocked. You have to close the process you spawned to get the return code.

    • example 2:

    try to run iisreset command with python

    >>> print subprocess.check_call("iisreset", shell = True)
    
    Attempting stop...
    Internet services successfully stopped
    Attempting start...
    Internet services successfully restarted
    0

     class Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)

    What the Popen returns is a child process you want to create.

    example 3:

    the Popen() process will not be blocked unless we explicit child.wait()

    import subprocess
    def havewait():
        child = subprocess.Popen(['ping','vwijin014'])
        child.wait()
        print 'parent process'
    
    def nowait():
        child = subprocess.Popen(['ping','vwijin014'])
        #child.wait()
        print 'parent process'

    Pay attention to the red line order. One is before the ping running.

  • 相关阅读:
    深入V8引擎-Time核心方法之win篇(2)
    深入V8引擎-Time核心方法之win篇(1)
    深入V8引擎-Time核心方法之mac篇
    深入V8引擎-Time模块介绍
    深入V8引擎-写在前面
    深入V8引擎-编译启动
    【机器学习】极大似然估计法
    【机器学习】贝叶斯决策论
    【机器学习】贝叶斯决策 实例
    【机器学习】贝叶斯公式
  • 原文地址:https://www.cnblogs.com/jin-wen-xin/p/5977392.html
Copyright © 2011-2022 走看看