python中target函数_python下Pool与target方法写在同一个类里要注意的坑

#!/usr/bin/evn python#_*_coding:utf8_*_

from multiprocessing importPool,Queue,Managerimporttime,subprocess,osclassYaCe(object):def __init__(self,api,binfile,maxpool,qu,maxrequest=100000,status="success"):

self.api=api

self.binfile=binfile

self.status=status

self.maxpool=maxpool

self.maxrequest=maxrequest

self.qu=qudefprorequest(self):for i inrange(self.maxrequest):

self.qu.put(i)print(i)for i inrange(int(self.maxpool)):

self.qu.put(None)print("None")defconumers(self,i):whileTrue:

data=self.qu.get(True)if data ==None:print("进程%s任务完成..."%i)break

else:#print("time curl -X POST '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))

command = subprocess.getoutput("time curl -X POST --connect-timeout 10 '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))#command = subprocess.getoutput("time curl -X POST '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))

if self.status == "success":

logfile= os.getcwd()+"/"+"headbeat.log"+"_%s"%date_timeif "CgoyMDAwMDAwMDAw" incommand:print("进程%s__%s..."%(str(i),str(data)))

with open(logfile,"a") as f:

f.write(command+"\n")

f.close()else:

with open(logfile,"a") as f:

f.write("Faild\n")

f.write(command+"\n")

f.close()else:

logfile= os.getcwd()+"/"+"roomlist.log"+"_%s"%date_time#print("time curl -X POST '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))

command = subprocess.getoutput("time curl -X POST --connect-timeout 10 '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))#command = subprocess.getoutput("time curl -X POST '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))

if "CAES+" incommand:print("进程%s__%s..."%(str(i),str(data)))

info= command.split('\n')[-3:]

info1= "\n".join(info)

with open(logfile,"a") as f:

f.write(info1+"\n")

f.close()else:print("进程%s__%s..."%(str(i),str(data)))

with open(logfile,"a") as f:

f.write("Faild\n")

f.write(command+"\n")

f.close()defmultirun(self):

ps= int(int(self.maxpool) - 1)

p=Pool(ps)for i inrange(self.maxpool):print("开启子进程%s"%i)

p.apply_async(conumers_wrapper,args=(self,i))#print(res.get)

print('等待所有添加的进程运行完毕。。。')

p.close()

p.join()

endtime= time.strftime("%Y%m%d_%X",time.localtime())if self.status == "success":

logfile= os.getcwd()+"/"+"headbeat.log"+"_%s"%date_timeelse:

logfile= os.getcwd() + "/" + "roomlist.log"+"_%s"%date_time

with open(logfile,"a") as f:

f.write("============[%s]============\n"%endtime)

f.close()print('End!!,PID:%s'%os.getpid())defconumers_wrapper(cls_instance,i):returncls_instance.conumers(i)if __name__ == "__main__":

q=Manager().Queue()

Yc= YaCe('压测接口','二进制证认文件',开多少个进程,queue(队列),maxrequest=100(模拟测试多少次访问),status="faild"(这里因为测试的两个接口,返回不一样用status参数区分测试的接口的返回值处理))

Yc.prorequest()print("++++++")globaldate_time

date_time= time.strftime("%Y%m%d_%X",time.localtime())

Yc.multirun()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值