python多线程与多进程--存活主机ping扫描以及爬取股票价格
python多线程与多进程
多线程:
案例:扫描给定网络中存活的主机(通过ping来测试,有响应则说明主机存活)
普通版本:
#扫描给定网络中存活的主机(通过ping来测试,有响应则说明主机存活)
import sys
import subprocess
import time
def ping(net,start=100,end=200,n=2,w=5):
for i in range(start,end+1):
ip=net+"."+str(i)
command="ping %s -n %d -w %d"%(ip,n,w)
print(ip,("通","不通")[subprocess.call(command,stdout=open("nul","w"))]) #stdout=open("nul","w") #不显示命令执行返回的结果
t1=time.time()
if len(sys.argv)!=2:
print("参数输入错误!")
print("运行示例:")
print("test01.py 123.125.114")
elif len(sys.argv)==2:
net=sys.argv[1]
ping(net)
t2=time.time()
print("程序耗时%f秒!"%(t2-t1)) #195.091611秒
运行效果如下:

在python里面,线程的创建有两种方式,其一使用Thread类创建
导入Python标准库中的Thread模块
from threading import Thread
创建一个线程
mthread = threading.Thread(target=function_name, args=(function_parameter1, function_parameterN))
启动刚刚创建的线程
mthread .start()
function_name: 需要线程去执行的方法名
args: 线程执行方法接收的参数,该属性是一个元组,如果只有一个参数也需要在末尾加逗号。
多线程版:
import sys
import subprocess
import time
from threading import Thread
#在python里面,线程的创建有两种方式,其一使用Thread类创建
# 导入Python标准库中的Thread模块
#from threading import Thread #
# 创建一个线程
#mthread = threading.Thread(target=function_name, args=(function_parameter1, function_parameterN))
# 启动刚刚创建的线程
#mthread .start()
#function_name: 需要线程去执行的方法名
#args: 线程执行方法接收的参数,该属性是一个元组,如果只有一个参数也需要在末尾加逗号。 result=[]
def ping1(ip):
command="ping %s -n 1 -w 20"%(ip)
result.append([ip,subprocess.call(command)])
def ping(net,start=100,end=200):
for i in range(start,end+1):
ip=net+"."+str(i)
th=Thread(target=ping1,args=(ip,))
th.start() def main():
if len(sys.argv)!=2:
print("参数输入错误!")
print("运行示例:")
print("test01.py 123.125.114")
elif len(sys.argv)==2:
net=sys.argv[1]
ping(net)
if __name__=='__main__':
t1=time.time()
main()
while len(result)!=101:
time.sleep(1)
print(result)
t2=time.time()
print("程序耗时%f秒!"%(t2-t1)) #1.585263秒
多线程案例2:爬取股票的价格
多线程
#爬取股票的价格
import requests
import re
import time
from threading import Thread code=[600016,600000,601939,600036,603683,600050,601890,600795,601857,600584,601231,603165,600644,603005,601198,603690,600643,600131,600776,603609,601377]
m1=re.compile(r"price: '(\d{1,3}\.\d{2}')")
def getprice(id):
url="http://quotes.money.163.com/0%s.html"%id
txt=requests.get(url).text
price=m1.search(txt).group(1)
print(id,price) if __name__=="__main__":
ts=[]
start=time.time()
for id in code:
t=Thread(target=getprice,args=(id,))
ts.append(t)
t.start()
for t in ts:
t.join() #等待子线程运行完,主线程再运行
print("程序耗时:",time.time()-start)
多进程:
爬取股票的价格(多进程版)
#多进程
#爬取股票的价格
import requests
import re
import time
from multiprocessing import Process
from threading import Thread
code=[600016,600000,601939,600036,603683,600050,601890,600795,601857,600584,601231,603165,600644,603005,601198,603690,600643,600131,600776,603609,601377]
m1=re.compile(r"price: '(\d{1,3}\.\d{2}')")
def getprice(id):
url="http://quotes.money.163.com/0%s.html"%id
txt=requests.get(url).text
price=m1.search(txt).group(1)
print(id,price)
ps=[] #进程池
if __name__=="__main__":
start=time.time()
for id in code:
p=Process(target=getprice,args=(id,))
ps.append(p) #把进程放入列表(进程池)
p.start() #启动进程
for p in ps:
p.join()
print(time.time()-start)
爬取股票的价格(多进程版)带Pool
#爬取股票的价格
import requests
import re
import time
from multiprocessing import Pool
#多进程带Pool code=[600016,600000,601939,600036,603683,600050,601890,600795,601857,600584,601231,603165,600644,603005,601198,603690,600643,600131,600776,603609,601377]
m1=re.compile(r"price: '(\d{1,3}\.\d{2}')")
def getprice(id):
url="http://quotes.money.163.com/0%s.html"%id
txt=requests.get(url).text
price=m1.search(txt).group(1)
print(id,price) if __name__=="__main__":
start=time.time()
p=Pool(4)
for id in code:
p.apply_async(getprice,args=(id,)) #async异步,第一个参数是函数名,第二个是此函数的参数
p.close()
p.join() #等待子线程运行完,主线程再运行
print("程序耗时:",time.time()-start)
python多线程与多进程--存活主机ping扫描以及爬取股票价格的更多相关文章
- Python多线程和多进程谁更快?
python多进程和多线程谁更快 python3.6 threading和multiprocessing 四核+三星250G-850-SSD 自从用多进程和多线程进行编程,一致没搞懂到底谁更快.网上很 ...
- python多线程与多进程及其区别
个人一直觉得对学习任何知识而言,概念是相当重要的.掌握了概念和原理,细节可以留给实践去推敲.掌握的关键在于理解,通过具体的实例和实际操作来感性的体会概念和原理可以起到很好的效果.本文通过一些具体的例子 ...
- Python 多线程、多进程 (三)之 线程进程对比、多进程
Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.多线程与多进 ...
- Python 多线程、多进程 (一)之 源码执行流程、GIL
Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.python ...
- Python 多线程、多进程 (二)之 多线程、同步、通信
Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.python ...
- 基于Windows平台的Python多线程及多进程学习小结
python多线程及多进程对于不同平台有不同的工具(platform-specific tools),如os.fork仅在Unix上可用,而windows不可用,该文仅针对windows平台可用的工具 ...
- 使用 Python 查看局域网内存活主机
1 安装 (如果误用了 pip insatll nmap的话,要先 pip uninstall nmap) pip install python-nmap Nmap 是一款用于网络发现和安全审计的网络 ...
- python 多线程、多进程
一.首先说下多线程.多进程用途及异同点,另外还涉及到队列的,memcache.redis的操作等: 1.在python中,如果一个程序是IO密集的操作,使用多线程:运算密集的操作使用多进程. 但是,其 ...
- python多线程,多进程
线程是公用内存,进程内存相互独立 python多线程只能是一个cpu,java可以将多个线程平均分配到其他cpu上 以核为单位,所以GIL(全局锁,保证线程安全,数据被安全读取)最小只能控制一个核,很 ...
随机推荐
- Django 系列博客(六)
Django 系列博客(六) 前言 本篇博客介绍 Django 中的路由控制部分,一个网络请求首先到达的就是路由这部分,经过路由与视图层的映射关系再执行相应的代码逻辑并将结果返回给客户端. Djang ...
- Runtime详解(下)
Runtime应用 1.Runtime 交换方法 应用场景:当第三方框架或者系统原生方法功能不能满足我们的时候,我们可以在保持系统原有功能的基础上,添加额外的功能. 需求:加载一张图片直接用系统的[U ...
- 在AspNetCore中扩展Log系列 - 介绍开源类库的使用(一)
转发时请注明原创作者及地址,否则追究责任. 原创:alunchen 当创建AspNetCore项目时 当我们创建一个AspNetCore项目时,需要我们手动添加Log: services.AddLog ...
- [转]nodejs中package.json和package-lock.json文件的功能分析
本文转自:https://blog.csdn.net/u013992330/article/details/81110018 最新版nodejs中,多了一个package-lock.json文件,刚开 ...
- 我的IdentityServer目录
概念部分 理解oauth协议 理解什么是claim 学习Identity Server 4的预备知识 Open ID Connect(OIDC)在 ASP.NET Core中的应用 操作部分 入门: ...
- Linux CentOS 虚拟机下联网
这里用VM 提供虚拟环境 虚拟机网络连接模式用的NAT 在编辑处 打开 查看你的子网ip 类型是NAT 的 我们这是是18 192.168.18.~ 去看dhcp 设置看自动分配的地址范围 也可 ...
- spring2.0:The server time zone value 'Ãùú±êüñ¼ä' is unrecognized or represents more than one time zone. You must configure either th
提示系统时区出现错误,可以在mysql中执行命令: set global time_zone='+8:00' 或者在数据库驱动的url后加上serverTimezone=UTC参数 jdbc:mysq ...
- 阅读Java Native源码前的准备
前言 读java native源代码时,我们一般会去网站下载openjdk8源码http://download.java.net/openjdk/jdk8/promoted/b132/openjdk- ...
- 【Tomcat】上线部署tomcat。常用命令
ps -ef | grep tomcat-web [查询tomact进程]kill -9 pid [结束tomcat进程]/opt/tomcat-web/bin/startup.sh [启动tomca ...
- Node.js性能分析神器Easy-Monitor
摘要: 使用Easy-Monitor,可以准确定位Node.js应用的性能瓶颈,帮助我们优化代码性能. 当应用出现性能问题时,最大的问题在于:如何准确定位造成性能瓶颈的代码呢?对于Node.js开发者 ...