2013-01-09 26 views
7

私は、subprocess.callを使ってPythonで外部アプリケーションを実行しようとしています。私が読んだところでは、Popprocess.allを呼び出さない限り、subprocess.callはブロックされませんが、私にとっては、外部アプリケーションが終了するまでブロックされています。これをどうやって解決するのですか?Python subprocess.call blocking

答えて

-1

subprocessのコードは、実際にはかなりシンプルで読みやすいです。 3.3または2.7バージョン(該当する場合)を見るだけで、それが何をしているのかを知ることができます。

例えば、callは次のようになります。

def call(*popenargs, timeout=None, **kwargs): 
    """Run command with arguments. Wait for command to complete or 
    timeout, then return the returncode attribute. 

    The arguments are the same as for the Popen constructor. Example: 

    retcode = call(["ls", "-l"]) 
    """ 
    with Popen(*popenargs, **kwargs) as p: 
     try: 
      return p.wait(timeout=timeout) 
     except: 
      p.kill() 
      p.wait() 
      raise 

あなたはwaitを呼び出さずに同じことを行うことができます。 Popenを作成し、waitを呼び出してはいけません。これはまさにあなたが望むものです。

+20

役立つが、納得がいく。 – dpitch40

5

あなたは誤ったドキュメントを読んでいます。それらによると:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) 

argsで説明されているコマンドを実行します。コマンドが完了するまで待ってから、returncode属性を戻してください。

+0

ああ、大丈夫です。どのようにして、os.spawnlをP_NOWAITオプションで呼び出すという機能を複製するのですか? – dpitch40

+1

@ dpitch40 - http://docs.python.org/2/library/subprocess.html#replacing-the-os-spawn-family。 –