2016-02-23 78 views
1

我試圖運行多個腳本,我有一個主腳本,我只是用名稱替換並運行。配置線程或多處理運行多個腳本python

的穿線方法我想要這個樣子的

from threading import Thread 
import sys 

sys.path.append('/python/loanrates/master') 

names = ['BTS', 'ETH', 'CLAM']#, 'DOGE', 'FCT', 'MAID', 'STR', 'XMR', 'XRP' ] 
threads = [] 
for name in names: 
    sys.path.append('/python/loanrates/'+name) 

import Master 

for name in names: 
    T = Thread(target=Master.main(name)) 
    print T 
    threads.append(T) 

for thread_ in threads: 
    thread_.start() 

for thread_ in threads: 
    thread_.join() 

但是,這只是開始的第一個腳本,即名「BTS」

的利用多重名字似乎簡單得多,但這時間它不認識池

import multiprocessing 
import Master 

pool = Pool(processes= 2) 

names = ['BTS', 'ETH']#, 'CLAM', 'DOGE', 'FCT', 'MAID', 'STR', 'XMR', 'XRP' ] 

pool.map(Master.main(), names) 

你會推薦哪個,我需要改變代碼以使其工作?

回答

0

你想發送一個函數和它的參數給線程,而不是實際在你的上下文中調用它。

所以更改此設置:

T = Thread(target=Master.main(name)) # actually calls Master.main(args) 

要這樣:

# send Master.main and arguemnts to the thread 
T = Thread(target=Master.main, args=(name,)) 

也是一樣Pool.map

更改此:

pool.map(Master.main(), names) 

更改爲:

pool.map(Master.main, names) 
0

嘗試從多所說的游泳池,

import multiprocessing as mp 
import Master 

pool = mp.Pool(processes= 2) 

names = ['BTS', 'ETH']#, 'CLAM', 'DOGE', 'FCT', 'MAID', 'STR', 'XMR', 'XRP' ] 

pool.map(Master.main, names) 

或負荷僅在特定

from multiprocessing import Pool 
import Master 

pool = Pool(processes= 2) 

names = ['BTS', 'ETH']#, 'CLAM', 'DOGE', 'FCT', 'MAID', 'STR', 'XMR', 'XRP' ] 

pool.map(Master.main, names) 
0

你必須去的線程構造函數的參數給出Master.main()參數args

所以創建線程情況下應該是:

for name in names: 
    T = Thread(target=Master.main, args=(name,)) 
    print T 
    threads.append(T) 

如果使用Thread(target=Master.main(name)Master.main()將在這一點上被調用,返回值將被用作目標值。