2017-01-17 12 views
-1

私はサブプロセスを通して自動化しようとしているLinuxスクリプトです。サブプロセスの各繰り返しは、親ディレクトリの各サブディレクトリでlinuxスクリプトを実行する必要があり、これらのサブプロセスはそれぞれ別のスレッドで実行する必要があります。スレッドを使ってサブプロセスを並行して実行する

次のように私のディレクトリが編成されている方法は次のとおりです。

  • /親/ P1 [n]は
  • /親/ P
  • までのように
  • /parent/p2....and

私のコードの最初の部分は、すべてのサブディレクトリ(p1、p2、p3 ...など)にわたってプロセスを実行することを目指しています。高速な処理にはうまくいきます。しかし、私の仕事の多くはバックグラウンドで実行する必要があります。私は通常nohupを使用して手動で別のノードで実行します。したがって、私の端末の各ノードは、各ディレクトリ(p1、p2、p3..etc)で同じジョブを実行します。私のコードの後半部分(スレッドを使用して)はこれを達成することを目指していますが、すべてのノードが同じプロセス(p1、p1、p1 ...など)を実行しています。私はそれらをスレッド上に分離したいときrunSims。誰かがどのように各ノードに異なるジョブを配置するためにスレッド機能をさらに反復できるか知っていますか?

import os 
import sys 
import subprocess 
import os.path 
import threading 

#takes the argument: python FOLDER_NAME #ofThreads 
#Example: python /parent 8 

directory = sys.argv[1] #in my case input is /parent 
threads = int(sys.argv[2]) #input is 8 
category_name = directory.split('/')[-1] #splits parent as a word 
folder_list = next(os.walk(directory))[1] #makes a list of subdirectories [p1,p2,p3..] 

def jobs(cmd): 
    for i in folder_list: 
     f = open("/vol01/bin/dir/nohup.out", "w") 
     cmd = subprocess.call(['nohup','python','np.py','{0}/{1}' .format(directory,i)],cwd = '/vol01/bin/dir', stdout=f) 
    return cmd 

def runSimThreads(numThreads): 
    threads = [] 
    for i in range(numThreads): 
     t = threading.Thread(target=jobs, args=(i,)) 
     threads.append(t) 
     t.start() 

#Wait for all threads to complete 
main_thread = threading.currentThread() 
for t in threads: 
    if t is main_thread: 
     continue 
    t.join() 

runSimThreads(threads) 

答えて

0

あなたのコードにすることはできません。

import os 
import sys 
import subprocess 
import os.path 
import threading 

#takes the argument: python FOLDER_NAME #ofThreads 
#Example: python /parent 8 

threads = 8 #input is 8 

... 
... 

for t in threads: 
    print("hello") 

--output:-- 
TypeError: 'int' object is not iterable 

あなたはどこでも同じ変数名を使用している、そしてそれはあなた混乱さ(または私を?)。

また、次の操作を行います。あなたは仕事は()のパラメータ変数を持つべきではないということを意味し、あなたのCMDパラメーター変数を、上書きされている

def jobs(cmd): 
    for i in folder_list: 
     f = open("/vol01/bin/dir/nohup.out", "w") 
     cmd = "something" 

。をコメント1する

応答:以下

import threading as thr 
import time 

def greet(): 
    print("hello world") 

t = thr.Thread(target=greet) 
t.start() 
t.join() 

--output:-- 
hello world 

import threading as thr 
import time 

def greet(greeting): 
    print(greeting) 

t = thr.Thread(target=greet, args=("Hello, Newman.",)) 
t.start() 
t.join() 

--output:-- 
Hello, Newman. 

あなたがやっていることと等価である:

import threading as thr 
import time 

def greet(greeting): 
    greeting = "Hello, Jerry." 
    print(greeting) 

t = thr.Thread(target=greet, args=("Hello, Newman.",)) 
t.start() 
t.join() 

--output:-- 
Hello, Jerry. 
そして、そのコードを読んで、誰が、なぜ「、頼みますgreet()関数を使用しないときに引数を渡していますか? "

I'm relatively new to python

さて、あなたのコードは、この行います

threads = 8 

#Other irrelevant stuff here 

for t in threads: 
    print("hello") 

をし、それがエラーを生成します:あなたは、なぜ

TypeError: 'int' object is not iterable

知っていますか?

+0

私はPythonには比較的新しいので、ここで私に同行してください。私は基本的にすべての私のsubprocess.callジョブのリスト(?)になる関数を定義しようとしており、スレッド関数を使って、前の関数から各ジョブを呼び出してスレッド上で実行することができます。現在、ジョブはパラレルではなくすべてのスレッドで直列に配置されます。(つまり、すべてのスレッドで同じジョブが実行されます)。だから私はスレッド関数で自分の引数を指定する方法を理解することができません。私はパラメータなしで関数を呼び出そうとしましたが、スレッディングの 'target ='部分に定義している間にエラーを投げました。 – Anuiyer

+0

@Anuiyer:_So私はスレッド関数で自分の引数を指定する方法を理解することができません._私のポストの下部を参照してください。 – 7stud

関連する問題