python多线程与多进程--存活主机ping扫描以及爬取股票价格
<div class="cnblogs_code"> ping(net,start=100,end=200,n=2,w=5 i range(start,end+1=net++=%(ip,(,)[subprocess.call(command,stdout=open(,))])
t1= len(sys.argv)!=2((( len(sys.argv)==2=sys.argv[1=(%(t2-t1))
运行效果如下: 在python里面,线程的创建有两种方式,其一使用Thread类创建导入Python标准库中的Thread模块 from threading import Thread?创建一个线程 mthread = threading.Thread(target=function_name,args=(function_parameter1,function_parameterN)) ?启动刚刚创建的线程 mthread .start()function_name: 需要线程去执行的方法名args: 线程执行方法接收的参数,该属性是一个元组,如果只有一个参数也需要在末尾加逗号。 多线程版: threading
==% ping(net,end=200 i range(start,end+1=net++=Thread(target=ping1,args=<span style="color: #0000ff">def<span style="color: #000000"> main():
<span style="color: #0000ff">if len(sys.argv)!=2<span style="color: #000000">: <span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">参数输入错误!<span style="color: #800000">"<span style="color: #000000">) <span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">运行示例:<span style="color: #800000">"<span style="color: #000000">) <span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">test01.py 123.125.114<span style="color: #800000">"<span style="color: #000000">) <span style="color: #0000ff">elif len(sys.argv)==2<span style="color: #000000">: net=sys.argv[1<span style="color: #000000">] ping(net) <span style="color: #0000ff">if <span style="color: #800080">name==<span style="color: #800000">'<span style="color: #800000">main<span style="color: #800000">'<span style="color: #000000">: t1=<span style="color: #000000">time.time() main() <span style="color: #0000ff">while len(result)!=101<span style="color: #000000">: time.sleep(1<span style="color: #000000">) <span style="color: #0000ff">print<span style="color: #000000">(result) t2=<span style="color: #000000">time.time() <span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">程序耗时%f秒!<span style="color: #800000">"%(t2-t1)) <span style="color: #008000">#<span style="color: #008000">1.585263秒 多线程案例2:爬取股票的价格
threading code=[600016,600000,601939,600036,603683,600050,601890,600795,601857,600584,601231,603165,600644,603005,601198,603690,600643,600131,600776,603609,601377<span style="color: #000000">]
m1=re.compile(r<span style="color: #800000">"<span style="color: #800000">price: '(d{1,3}.d{2}')<span style="color: #800000">"<span style="color: #000000">) <span style="color: #0000ff">def<span style="color: #000000"> getprice(id): url=<span style="color: #800000">"<span style="color: #800000">http://quotes.money.163.com/0%s.html<span style="color: #800000">"%<span style="color: #000000">id txt=<span style="color: #000000">requests.get(url).text price=m1.search(txt).group(1<span style="color: #000000">) <span style="color: #0000ff">print<span style="color: #000000">(id,price) <span style="color: #0000ff">if <span style="color: #800080">name==<span style="color: #800000">"<span style="color: #800000">main<span style="color: #800000">"<span style="color: #000000">:
爬取股票的价格(
multiprocessing threading =[600016,3}.d{2}')=%==m1.search(txt).group(1=[]
=== id =Process(target=getprice,))
ps.append(p)
p.start()
p (time.time()-start)
爬取股票的价格(多进程版)带Pool
multiprocessing
=[600016,3}.d{2}')=%==m1.search(txt).group(1<span style="color: #0000ff">if <span style="color: #800080">name==<span style="color: #800000">"<span style="color: #800000">main<span style="color: #800000">"<span style="color: #000000">: start=<span style="color: #000000">time.time() p=Pool(4<span style="color: #000000">) <span style="color: #0000ff">for id <span style="color: #0000ff">in<span style="color: #000000"> code: p.apply_async(getprice,args=(id,)) <span style="color: #008000">#<span style="color: #008000">async异步,第一个参数是函数名,第二个是此函数的参数 <span style="color: #000000"> p.close() p.join() <span style="color: #008000">#<span style="color: #008000">等待子线程运行完,主线程再运行 <span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">程序耗时:<span style="color: #800000">",time.time()-start) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |