【发布时间】:2015-06-08 10:26:58
【问题描述】:
对于我的大学项目,我正在尝试开发一个基于 python 的流量生成器。我在 vmware 上创建了 2 台 CentOS 机器,我使用 1 作为我的客户端和 1 作为我的服务器机器。我已经使用IP aliasing 技术来增加仅使用单个客户端/服务器机器的客户端和服务器的数量。到目前为止,我已经在我的客户端机器上创建了 50 个 IP 别名,在我的服务器机器上创建了 10 个 IP 别名。我还使用多处理模块同时生成从所有 50 个客户端到所有 10 个服务器的流量。我还在我的服务器上开发了一些配置文件(1kb、10kb、50kb、100kb、500kb、1mb)(在 /var/www/html 目录中,因为我使用的是 Apache 服务器)并且我正在使用 urllib2 向这些配置文件发送请求我的客户端机器。我使用httplib+urllib2首先绑定到任何一个源别名ip,然后使用urllib2从这个ip发送请求。这里到increase my number of TCP Connections,我正在尝试使用 multiprocessing.Pool.apply_async 模块。但是我在运行我的脚本时收到此错误“RuntimeError:同步对象只能通过继承在进程之间共享”。经过一番调试,我发现这个错误是由于使用了multiprocessing.Value引起的。但是我想在我的进程之间共享一些变量,并且我还想增加我的 TCP 连接数。在这里可以使用哪些其他模块(除了 multiprocessing.Value)来共享一些公共变量?或者这个查询还有其他解决方案吗?
'''
Traffic Generator Script:
Here I have used IP Aliasing to create multiple clients on single vm machine.
Same I have done on server side to create multiple servers. I have around 50 clients and 10 servers
'''
import multiprocessing
import urllib2
import random
import myurllist #list of all destination urls for all 10 servers
import time
import socbindtry #script that binds various virtual/aliased client ips to the script
m=multiprocessing.Manager()
response_time=m.list() #some shared variables
error_count=multiprocessing.Value('i',0)
def send_request3(): #function to send requests from alias client ip 1
opener=urllib2.build_opener(socbindtry.BindableHTTPHandler3) #bind to alias client ip1
try:
tstart=time.time()
for i in range(myurllist.url):
x=random.choice(myurllist.url[i])
opener.open(x).read()
print "file downloaded:",x
response_time.append(time.time()-tstart)
except urllib2.URLError, e:
error_count.value=error_count.value+1
def send_request4(): #function to send requests from alias client ip 2
opener=urllib2.build_opener(socbindtry.BindableHTTPHandler4) #bind to alias client ip2
try:
tstart=time.time()
for i in range(myurllist.url):
x=random.choice(myurllist.url[i])
opener.open(x).read()
print "file downloaded:",x
response_time.append(time.time()-tstart)
except urllib2.URLError, e:
error_count.value=error_count.value+1
#50 such functions are defined here for 50 clients
def func():
pool=multiprocessing.Pool(processes=750)
for i in range(5):
pool.apply_async(send_request3)
pool.apply_async(send_request4)
pool.apply_async(send_request5)
#append 50 functions here
pool.close()
pool.join()
print"All work Done..!!"
return
start=float(time.time())
func()
end=float(time.time())-start
print end
【问题讨论】:
-
response_time,是你的共享变量,不是吗? -
使用
multiprocessing.Queue,而不是一个全局变量IMO。 -
如果我不使用共享变量 response_time 和 error_count,Pool.apply_async 可以完美运行。
-
@pnv yes response_time 和 error_count 是我的共享变量。
-
@pnv 我从未使用过 multiprocessing.Queue。你能指导我如何在这里使用 multiprocessing.Queue 吗?
标签: python linux pool python-multiprocessing