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.

  • 相关阅读:
    VMware Workstation-虚拟机的安装
    Linux操作系统-命令-free
    Linux操作系统-命令-vmstat
    抛出一个异常
    字符集
    页面缓存
    表单域
    弹出式说明
    canselBubble 和 stopPropagation理解
    JS---递归函数理解
  • 原文地址:https://www.cnblogs.com/jin-wen-xin/p/5977392.html
Copyright © 2011-2022 走看看