如何将此Python代码更改为多进程而不是多线程?
下面的Python代码连接到许多服务器,从每个服务器获取一些信息并返回结果。它当前为每个连接启动一个单独的线程。我想了解每个连接使用单独的进程而不是线程对性能的影响。这个代码可以很容易地更改为使用进程而不是线程吗?我到底需要做什么?如果有风险,风险是什么 Python 2.6/Linux平台如何将此Python代码更改为多进程而不是多线程?,python,multithreading,multiprocessing,Python,Multithreading,Multiprocessing,下面的Python代码连接到许多服务器,从每个服务器获取一些信息并返回结果。它当前为每个连接启动一个单独的线程。我想了解每个连接使用单独的进程而不是线程对性能的影响。这个代码可以很容易地更改为使用进程而不是线程吗?我到底需要做什么?如果有风险,风险是什么 Python 2.6/Linux平台 class ServerInfoGetter(threading.Thread): def __init__(self, host, port=DEFAULT_PORT, timeout=15):
class ServerInfoGetter(threading.Thread):
def __init__(self, host, port=DEFAULT_PORT, timeout=15):
self.host = host
self.timeout = timeout
self.port = port
self.result = None
threading.Thread.__init__(self)
def get_result(self):
return self.result
def run(self):
try:
serv_check = ServCheck(self.host, \
port=self.port, \
timeout=self.timeout)
serv_check.get_info()
self.result = serv_check
except Exception, err:
logging.debug("Could not run ServCheck for : %s %s",self.host, err)
def process_hosts(hosts_and_ports):
def producer(queue, hosts_and_ports):
for host, ports in hosts_and_ports.items():
for port in ports:
logging.info("processing host: %s:%s", host, port)
thread = ServerInfoGetter(str(host), port)
thread.start()
queue.put(thread, True) # True so block until slot available
results = []
def consumer(queue, total_checks):
while len(results) < total_checks:
thread = queue.get(True)
thread.join()
results.append(thread.get_result())
logging.info("processing hosts")
queue = Queue(QUEUE_SIZE)
prod_thread = threading.Thread(target=producer,
args=(queue,
hosts_and_ports))
cons_thread = threading.Thread(target=consumer,
args=(queue,
calculate_total_checks(hosts_and_ports)))
prod_thread.start()
cons_thread.start()
prod_thread.join()
cons_thread.join()
return results
classserverinfogetter(threading.Thread):
def u u init(self,host,port=DEFAULT_port,timeout=15):
self.host=host
self.timeout=超时
self.port=端口
self.result=None
threading.Thread.\uuuuu init\uuuuuu(自)
def get_结果(自身):
返回自我结果
def运行(自):
尝试:
serv_check=ServCheck(self.host\
端口=self.port\
超时=自身。超时)
服务检查。获取信息()
self.result=服务检查
除例外情况外,错误:
logging.debug(“无法为%s%s运行ServCheck”,self.host,错误)
def进程_主机(主机_和_端口):
def生产者(队列、主机和端口):
对于主机,主机中的端口\u和\u端口。项()
对于端口中的端口:
logging.info(“处理主机:%s:%s”,主机,端口)
thread=ServerInfoGetter(str(主机),端口)
thread.start()
queue.put(thread,True)#True,因此阻塞直到插槽可用
结果=[]
def消费者(队列、总检查):
而len(results)
您是否考虑过使用单进程和单线程,例如使用?
只有当os.fork可用时,多进程选项才可能相当容易……您是否考虑过使用单进程和单线程,例如使用? 只有当os.fork可用时,多进程选项才可能相当简单。…如中所述:
多处理
是一个软件包,它支持使用类似于线程化
模块的API生成进程。[…]在多处理
中,通过创建进程
对象,然后调用其start()
方法生成进程。进程遵循线程的API。线程
因此,基本上,您只需将所有threading.Thread
对象替换为multiprocessing.Process
对象(同样,队列需要替换为multiprocessing.queue
对象)
至少,看起来是这样的。然而,在实践中,所有需要跨越处理
边界的对象都必须是对象。否则,它们将永远不会跨线程更新
这包括self.host
,self.timeout
,self.port
,self.result
,如果您只想修改ServerInfoGetter
类。阅读多处理文档的其余部分,了解需要使用的其他数据类型
另外,作为旁注,我不确定Linux上的Python2.6是否会有问题,但对于Windows上的Python2.7,idle和交互式解释器在多处理方面都有问题(至少对我来说是这样)。当直接使用python或pythonw可执行文件执行脚本时,这些问题就消失了更新-我的Slackware box上的python 2.5.1没有这个问题,所以您在交互模式下也可以。。。虽然winwaed不是,但谁知道呢…?正如它在:
多处理
是一个软件包,它支持使用类似于线程化
模块的API生成进程。[…]在多处理
中,通过创建进程
对象,然后调用其start()
方法生成进程。进程遵循线程的API。线程
因此,基本上,您只需将所有threading.Thread
对象替换为multiprocessing.Process
对象(同样,队列需要替换为multiprocessing.queue
对象)
至少,看起来是这样的。然而,在实践中,所有需要跨越处理
边界的对象都必须是对象。否则,它们将永远不会跨线程更新
这包括self.host
,self.timeout
,self.port
,self.result
,如果您只想修改ServerInfoGetter
类。阅读多处理文档的其余部分,了解需要使用的其他数据类型
另外,作为旁注,我不确定Linux上的Python2.6是否会有问题,但对于Windows上的Python2.7,idle和交互式解释器在多处理方面都有问题(至少对我来说是这样)。当直接使用python或pythonw可执行文件执行脚本时,这些问题就消失了更新-我的Slackware box上的python 2.5.1没有这个问题,所以您在交互模式下也可以。。。虽然winwaed不是,但谁知道…?对不起,我应该说我在用蟒蛇