1

を実行に失敗Pythonのマルチプロセッシングは

OSError: [Errno 2] No such file or directory

奇妙な問題があること、私は、このような-aなど他のフラグなしでLSを実行しようと、サブプロセスの実行意図したとおりのエラーは発生しません。私はまた、フラグ= -aと一緒にshell = Trueを追加しようとしましたが、まだ運がありません。

from multiprocessing import * 
import subprocess 


class ThreadManager: 
    def __init__(self, tools): 
     self.tools = tools 
     self.pool_size = cpu_count() 
     self.p1 = Pool(processes=self.pool_size, maxtasksperchild=2,) 

    def initiate(self): 
     for self.tool in self.tools: 
      print self.tool 
      self.p1 = Pool(4) 
      self.p1 = Process(target=subprocess.call, args=(self.tool,)) 
      print self.p1 

      self.p1.start() 

th = ThreadManager("ls -a".split("/")) 
th.initiate() 
+0

'subprocess.call'は' "LS -a" 'で仕事をしたり、' [ "/ binに/のように渡された引数のリストを持っているために、'シェル= true'を使って呼び出されるべきls "、" -a "]'と入力します。なぜsplit( "/")を使うのですか – myaut

+0

私は 'split("/")'を使わずにこれを実行すると結果が出るので、 'l s - a'という文字列を使います。私は傾けることができない=本当の作業をシェル。つまり、これを実行することです 'Process(target = subprocess.call、args =(self.tool、shell = True))' ** ** –

+0

を呼び出す関数を割り当てることができません 'shell = True'は_キーワード_(_kwarg_)であり、 'args'は_postional引数_のタプルを受け入れます。 'ls -a"を '[" ls "、" -a "に変換する' .split() 'を'プロセス 'に渡すために' kwargs = dict(shell = True) "]' – myaut

答えて

0

あなたの問題はここにある:

"ls -a".split("/") 

これは、存在しない "LS -a" という名前のバイナリを見つけるために)subprocess.callを(強制的にリスト["ls -a"]に変換します。 shell=Trueキーワード引数であるべきでありながらsubprocess.callの最初の引数は()、位置引数argsタプルとして渡すこと

subprocess.call("ls -a", shell=True) # argument parsing is done by shell 
subprocess.call(["ls", "-a"])   # argument parsing not needed 
subprocess.call("ls -a".split())  # argument parsing is done via .split() 
subprocess.call(shlex.split("ls -a")) # a more reliable approach to parse 
             # command line arguments in Python 

注:二つの方法subprocess.call()を呼び出すことができ辞書引きkwargsとして渡すこと:

Process(target=subprocess.call, args=("ls -a",), kwargs=dict(shell=True)) 
Process(target=subprocess.call, args=(["ls", "-a"],)) 
関連する問題