2011-12-22 42 views
2

我想使用python的多進程模塊運行多個gearman工作進程,但似乎進程正在順序模式下執行。如果我在幾個終端中運行單獨的worker.py程序,那麼它工作正常。但是我想減輕在很多終端中手動指定worker.py的負擔。有沒有其他辦法呢?在python中使用多進程模塊運行多個Gearman進程

import sys , os , simplejson 
from fabric import * 
from fabric.api import * 
import gearman 
from gearman import GearmanWorker 
from multiprocessing import Pool 


##--Global Variables--## 
#Spawing minimun 5 worker threads for Gearman 



#executing the Job. gmJob consist of dict[host , cmd , pass] 
def exe_job(gmWorker , gmJob): 
print " the worker process is " , os.getpid() 
d = simplejson.loads(gmJob.data) 
env.host_string = d['host'] 
env.password = d['pass'] #will store the password . 
cmds = d['cmd'] 
print cmds 
for i in cmds: 
    sudo (i) # using fabric functions to ssh into system 
return "job sucessfull" 

def start_exe(): 
#woker node id to be specified in here 
gm_worker = gearman.GearmanWorker(['localhost:4730']) 
#gm_worker.set_client_id('client1') 
gm_worker.register_task('exe_job',exe_job) 
gm_worker.work() 


if __name__ == '__main__': 
p = Pool(5) 
result = p.apply_async(start_exe) 
print result.get() 

回答

1

我無法找到一個答案,要麼,所以我挖成的事情,想通了,你基本上用一個隊列來跟蹤你有哪些進程打開和關閉了(在一個錯誤一名齒輪工人的情況)。無論如何,我將它構建成一個模塊並將其發佈到pypi上。它仍然是非常進展中的工作,但我會盡力去的第二天或因此增加文檔和示例:

我還包括客戶端和工人通過json進行通信的類(我提到這是因爲你的例子似乎使用json)。

讓我知道你在想什麼。我絕對可以用更多的眼光來發現錯誤,或者告訴我我在代碼中做了什麼完全瘋狂的事情。