2011-12-22 76 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)。

让我知道你在想什么。我绝对可以用更多的眼光来发现错误,或者告诉我我在代码中做了什么完全疯狂的事情。