python 并发没有听懂,老师能否对解决这个案例,这个用go写两秒执行完成,py需要10多秒
来源:11-12 本章小结

勤劳的小蜜蜂zj
2018-08-07
# __*__ coding: utf-8 __*__
# __author__ = 'zj'
# __date__ = '2018/7/18 9:27'
import threading
import time
import requests
from multiprocessing import Process, Queue, Pool, Manager, Pipe
from requests import exceptions
import logging
import MySQLdb
import sys
# reload(sys)
# sys.setdefaultencoding('utf8')
mess = []
timeouterror = []
urldatas = [('来客优', 'http://likeyoumall.chendind.com/static/download.html?show=1&agentid=30000129'), ('金粒得', 'https://jldtg.dahemap.com/t/2058.htm'), ('惠花花', 'https://at.umeng.com/5rSX1z'), ('佩奇快借', 'http://peiqi.youzi365.cn/landing/?channel=gg1'), ('财富多', 'https://cfdtg.seercf.com/t/2009.htm'), ('星月宝', 'https://xingyue.jsd0086.com/home/register/3wg7z9ico68rii61zchwsb48wkquac3t/'), ('金易借', 'http://m.jinquqianbao.com/apps/SCGV/QM48HB/1532067031041'), ('美丽人生¥', 'http://mlrsapi1.jrwljd.com/h5/invite.jsp?invitationCode=null&channelCode=daikuanguanjia'), ('天猫白卡', 'https://tmbk-agent.ifootstone.com/mobile/gotodownload?2ACB75B6111194F8349FD66BAA56767E='), ('借帮帮', 'https://jiebangbang.cn/jie/?sourceId=021'), ('米米白卡', 'https://tui.xianjinbaobei1.com/#/spread?bcode=C5uuHqXyqfu'), ('豌豆钱包', 'http://wdh5.xwandoumall.com/wandoumall_h5/wandou_daoliu/register.html?channelId=zhongyihua'), ('51极速借', 'http://t.cn/RdnoBf8'), ('征征贷', 'https://sensors-api.kongapi.com/r/K'), ('花赢宝', 'https://huayingbao-agent.yylky.cn/mobile/gotodownload?339C24195F9AE79E7665948162945C0E96CDC7F05950E41B93EA34B246441DE9='), ('马上有钱', 'http://www.ytrong.com/?channelId=16'), ('贷去浪', 'https://dql-agent.ifootstone.com/mobile/gotodownload?814D11848A2D5066C0D52B4F5A0B770908DB273948A1A228490BEEBD80777647'), ('51极速借¥', 'http://t.cn/RdnoBf8'), ('广源钱包', 'http://gy.spjdl.com/Home/Register?code=fb042b9501a0487e93243a9995860064&viewName=gy'), ('信用额', 'https://sday-api.xiaozhuqiandai.com/h5/FTH5/register.html?channelCode=dkgj'), ('千牛钱包', 'http://dw11.vip/s/jeb'), ('随心花', 'https://h5.cangnanmingwei.com/sxhRegister-ChannelBak.html?QD4049'), ('富临门', 'http://flmtg.dahemap.com/t/2009.htm'), ('荣幸回收', 'http://app.xmrxzl.com/share/index.html?channelName=xiaoxie01'), ('今日借', 'https://12i.cn/MCq'), ('移动钱箱', 'https://www.ywcaiyuanwang.com/home/register/7b4v8c7r/'), ('小果钱包', 'http://fzjhtwo.suangzang.com:8090/code/market_qgj_35.html'), ('会盟金融', 'http://query.lianfen360.com/borrow?marketId=mk603fs2'), ('花乐享', 'http://app.hswansheng.com:8181/hlx/index.html?qcode=67'), ('极速信用', 'https://qlytg.seercf.com/t/2248.htm'), ('微贷101', 'http://wd101.michlhole.cn/tui/regist?chanel=qiangj'), ('么么哒钱包', 'https://oolaile.com/memeda/infocoll.html?cl=hd'), ('容易借', 'http://www.casheasy.cn/ECAHybrid/www/html/index.product.module.html?channel=dk'), ('守信网商', 'https://xx.yinfeilicai.com/yflc/regist3?code=3'), ('易借口袋', 'https://yjkd.fanzhoutech.com/mobile/phoneverification?par=6123A206CB30580C9290B166E65F4AC1'), ('花乐享¥', 'http://app.hswansheng.com:8181/hlx/index.html?qcode=67'), ('米米借', 'https://mmjtg.dahemap.com/t/2248.htm'), ('熙利行', 'https://12i.cn/utQ'), ('花花档', 'https://hhd-api.wuhuyinyuan.com/h5/FTH5/register.html?channelCode=dkgj'), ('星易花', 'https://xingyihua.fanzhoutech.com/mobile/phoneverification?par=2EC5A19C93D65B447E2101D6D99A7E86'), ('量贩贷', 'http://www.liangfand.com/index.html?channel=zf05'), ('星速钱包', 'http://xsqb.facaishu7.com/m/#/?sign=n2wFBqcnrbYi'), ('奇灵钱包', 'https://qlqgtg.seercf.com/t/2199.htm'), ('八秒贷$', 'http://baihui.bhuidai.com/wap/v1/anon/h5/page?channel=1034'), ('天猫白卡$', 'https://tmbk-agent.ifootstone.com/mobile/gotodownload?692C52E7F71DB9C845FBAFB1D46CB5B108DB273948A1A228490BEEBD80777647='), ('财富多$', 'https://cfdtg.seercf.com/t/2061.htm'), ('一起花¥', 'http://www.feiniaozu.com.cn/web/channel/e8q73up5ob2y'), ('蜂窝贷', 'https://bee.geziqianbao.com/yimiaojie/inborrow/redirectindex?cid=268552915#&/yimiaojie/passport/register?cid=268552915'), ('一起花', 'http://www.feiniaozu.com.cn/web/channel/e8q73up5ob2y'), ('零花钱$', 'http://www.mjjdpt.com:8082/saas/index.html?appId=marketentry&marketAppId=com.lhq&extensionWay=0&wayName=%E8%B4%B7%E6%AC%BE%E7%AE%A1%E5%AE%B61&channelId=201807310000324&appName=%E9%9B%B6%E8%8A%B1%E9%92%B1'), ('用钱花花', 'https://www.nlbt3.cn/yqdssmall/reg.html?code=G00602'), ('金马小袋', 'http://shandiankoudai2.xingyunqianbao.net/Fast/index/index/source/a8d5'), ('金元宝', 'http://jybapp.guoji1818.com/jyb_share/?channelName=song17'), ('金口贷', 'http://dazhongsudai.1gsw.com/pop/h5/weixin?channel=10095'), ('优易借', 'https://yyjtg.dahemap.com/t/2074.htm'), ('花啦啦', 'http://hualala8.com/?agentSign=57b27a536d2b4ab3827a8f7d46473878'), ('金银小袋', 'http://jinyinxiaodai2.xingyunqianbao.net/Fast/index/index/source/ece1'), ('金易借¥', 'http://m.jinquqianbao.com/apps/SCGV/QM48HB/1532067031041'), ('安安现金', 'https://www.kuiwoye.com:8888/regist/app.html?code=ybw04'), ('飞凡优品', 'http://ffyp.dazhoujiafankeji.com/borrowUser/toTgRedister?code=eab2cb255c7f76dc3efa8183c16052a2'), ('海豹商城', 'http://hbh5.xhaibao.com/haibaomall_h5/haibao_daoliu/register.html?channelId=zhongyihua'), ('领点花', 'http://designdb.cn/action/basicMember/extension?uuid=20180803150135033001'), ('小钞花花', 'https://xchhtg.seercf.com/t/2039.htm'), ('周享贷', 'http://m.zj.nbzhcl3010.com/apps/5LOU/8U5WPS/1533098820844'), ('蚂蚁金宝', 'https://myjbtg.seercf.com/t/2003.htm'), ('银码头', 'http://channel.ymt.nongyaodai.com/#/pre/guide?source=hsd52'), ('开心钱袋', 'http://reg.happy.hxlease.net/?aid=306&'), ('现金宝', 'https://xjb.fanzhoutech.com/mobile/phoneverification?par=87D0D7081DE32D68F2FE24F6643E4339'), ('金粒得$', 'https://jldtg.dahemap.com/t/2081.htm'), ('简速贷', 'https://taotie.shtmedia.com/invite/baoxian?source=027'), ('小牛牛', 'https://xnnh5.mymeifu.com/?channel_id=105'), ('小时代', 'https://xiaoshidai.jsd0086.com/home/register/y4m07w7yzyhm6ro5bsttkakfxzagut11/'), ('虎虎钱包', 'http://kuaizu.xiyoukeji.com/static/download.html?show=1&agentid=1264020'), ('优米花', 'http://www.zweirz.com:8181/ymh/index.html?qcode=036'), ('财富通', 'https://cfttg.dahemap.com/t/2039.htm'), ('易速小贷', 'https://ysxd.fanzhoutech.com/mobile/phoneverification?par=8A902DE73D24BA1564945CCFD0090933'), ('澜山信用', 'http://admin.jiexiabei.com/mobile/public/register?channel=dkgj'), ('快乐贷', 'http://www.kld.hxlease.net/h5/register.html?agentId=272'), ('周润花', 'https://zrh.fanzhoutech.com/mobile/phoneverification?par=E7209DD17257A7D5BC1B82B70FA879A9'), ('毛毛虫', 'https://mmctg.dahemap.com/t/2016.htm'), ('美丽人生', 'http://mlrsapi1.jrwljd.com/h5/invite.jsp?invitationCode=null&channelCode=daikuanguanjia'), ('鑫瑞信用', 'http://xinruixinyong.kfdehao.com/pop/h5/weixin?channel=10079'), ('普惠金融', 'http://www.renrenyoutiao.com/wap/login?inCode=shxx4'), ('金牛宝', 'http://register.jnb.hxlease.net/?aid=247'), ('农夫山钱', 'https://www.nbleixiang.com/home/register/srxfa0yw/'), ('优品钱袋', 'https://ypqdtg.dahemap.com/t/2016.htm'), ('立即贷', 'https://jkd.2345.com/m/register/ljd14/index.html?channel=ljd-daikuanguanjia02_cpk_zbc'), ('秒贷王', 'http://b.creditxb.com/?h=v5e5GO'), ('米米借$', 'https://mmjtg.dahemap.com/t/2247.htm'), ('零花钱', 'http://t.cn/RgT7TVp'), ('花掌柜', 'https://www.nbduolaimi.com/home/register/yqvfiw1t/'), ('点币达', 'https://dbdtg.seercf.com/t/2744.htm'), ('闪电虎', 'https://sdh.hzlixinxinxi.com/jm-app/static/h5/sdh/register.html?chnl=34'), ('淘钱宝', 'https://m.taoqian123.com/promote/view?id=972&channel_id=2084'), ('天瑞钱包', 'http://www.57zyz.cn/Enjoy/template/reg.html?&t=7'), ('桔子宝', 'https://mx.adrtb.net/backstage/Backstage375.html')]
class GetCode():
def __init__(self, urldata):
self.urldata = urldata
def ReqUrl(self):
try:
get_url = requests.get((self.urldata[1]), allow_redirects=False, timeout=1)
code = ("%s状态:%d。URL地址:%s") % (self.urldata[0], get_url.status_code, self.urldata[1])
mess.append(code)
except exceptions.Timeout as e:
code = ("%s异常:%s。URL地址:%s") % (self.urldata[0],e, self.urldata[1])
timeouterror.append(code)
except exceptions.HTTPError as e:
code = ("%s异常:%s。URL地址:%s") % (self.urldata[0], e, self.urldata[1])
timeouterror.append(code)
except:
code = ("%s异常。URL地址:%s") % (self.urldata[0], self.urldata[1])
timeouterror.append(code)
if __name__ == '__main__':
start_time = time.time()
queue = Queue(10)
for Url in range(len(urldatas)):
urldata = urldatas[Url]
get_url = GetCode(urldata).ReqUrl()
# thread_detail_url = threading.Thread(target=get_url, args=("10",))
thread_detail_url = Process(target=get_url, args=(queue,))
thread_detail_url.start()
print(time.time() - start_time)
print(mess)
print(len(mess))
# print("------------------")
print(timeouterror)
2回答
-
Necromancer_
2021-12-31
io用线程会快一点
00 -
bobby
2018-08-08
能格式化一下代码吗?我这看着样式是乱的, 我在我这拷贝运行一下试试
00
相似问题