2017-01-17 32 views
-1

我正在尋找一個通過子進程自動化的linux腳本。子進程的每次迭代都應該在父目錄的每個子目錄中運行linux腳本,並且每個子進程都應該在單獨的線程中運行。使用線程並行運行子進程

我的目錄組織方式如下:

  • /父母/ P1
  • /parent/p2....and以此類推,直到
  • /父母/ P [N]

我的代碼的第一部分旨在跨所有子目錄(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 

您正在使用相同的變量名無處不在,而且是混淆你(或我?)。

你也這樣做:

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

您將要覆蓋CMD參數變量,這意味着就業機會()不應該有一個參數變量。

響應註釋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:_因此我無法弄清楚如何在線程函數中指定我的參數._請參閱我的帖子的底部。 – 7stud