`
- 浏览:
904444 次
-
在网上看到了好多关于python的thread的问题,关于多线程的爬虫,我发现网上一些论坛的代码实际上是单线程的,而我也是刚刚开始搞多线程,这是我的理解,请各位高手多多指教,前面是论坛代码,后面是我自己写的代码,新手,还请指点一下,一起进步~
论坛代码(自己研究了一下,大体是这样):
#encoding:UTF-8
importthreading
importurllib,urllib2
importQueue
importbs4
frombs4importBeautifulSoup
importtime
hosts=['http://www.baidu.com','http://www.weibo.com','http://www.renren.com']
queue=Queue.Queue(0);
out_queue=Queue.Queue(0)
classContentThread(threading.Thread):
def__init__(self,queue,out_queue):
threading.Thread.__init__(self)
self.queue=queue
self.out_queue=out_queue
defrun(self):
whileTrue:
host=self.queue.get()
url=urllib.urlopen(host)
chunk=url.read()
self.out_queue.put(chunk)
self.queue.task_done()
classmatch(threading.Thread):
def__init__(self,out_queue):
threading.Thread.__init__(self)
self.out_queue=out_queue
defrun(self):
whileTrue:
chunk=self.out_queue.get()
soup=BeautifulSoup(chunk)
printsoup.findAll(['title'])
printself.name
self.out_queue.task_done()
start=time.time()
defmain():
t=ContentThread(queue,out_queue)
t.setDaemon(True)
t.start()
foriinhosts:
queue.put(i)
dt=match(out_queue)
dt.setDaemon(True)
dt.start()
queue.join()
out_queue.join()
if__name__=='__main__':
main()
print'爬虫耗时:%s秒'%(time.time()-start)
这个代码在task_done()之后加上time.sleep(10)会有明显的堵塞情况。输出threadname也是显示相同的线程名字。
这是是我写的多线程爬虫:
#encoding:UTF-8
importthreading
importurllib,urllib2
importQueue
importbs4
frombs4importBeautifulSoup
importtime
queue=Queue.Queue(0)
hosts=['http://www.baidu.com','http://www.weibo.com','http://www.renren.com']
classMyThread(threading.Thread):
def__init__(self):
threading.Thread.__init__(self)
self.queue=queue
#self.out_queue=out_queue
defrun(self):
globalqueue
host=self.queue.get()
printself.name
url=urllib.urlopen(host)
chunk=url.read()
soup=BeautifulSoup(chunk)
printsoup.findAll(['title'])
print'爬虫耗时:%s秒'%(time.time()-start)
#printhost
#printqueue.qsize()
start=time.time()
defhost():
foriinhosts:
t=MyThread()
t.start()
msg=str(i)
globalqueue
queue.put(str(i))
printmsg
#print"ElapsedTime:%s"%(time.time()-start)
if__name__=='__main__':
host()
这个在输出时间之后再sleep不会出现堵塞情况,输出threadname也是显示不同的线程名字
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
主要为大家详细介绍了python多线程同步之文件读写控制,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Python 多线程编程实例,一个综合示例
基于Linux的python多线程爬虫程序设计.pdf
python多线程压测demo
python 多线程封装脚本,可以直接拿来当lib导入使用。
Python里的多线程是假的多线程,不管有多少核,同一时间只能在一个核中进行操作!利用Python的多线程,只是利用CPU上下文切换的优势,看上去像是并发,其实只是个单线程,所以说他是假的单线程。 那么什么时候用多...
一个Python多线程爬虫,在工作时,开10个线程来抓取新浪网页的数据,抓取并保存页面, 并且根据deep返回页面链接,根据key确定是否保存该页面,其中: deep == 0时,是抓取的最后一层深度,即只抓取并保存页面,不...
python多线程编程实现网络串口透传, 为TCP客户端网口数据串口透传。
python 多线程 实例 注释详解
Python多线程编程文档说明 多进程编程 一、 multiprocessing 模块 1. multiprocessing 模块提供了多进程编程的能力 它的API非常类似于 threading 模块,但是也提供了一些threading 模块不具有的能力 相比于线程,它...
提供了使用python多线程与但线程下载在线地图瓦片的源码。多线程方式,经过实测检验,可以稳定长时间运行,下载失败,自动重试。python3.0环境
python多线程,断点续传下载程序,功能比较简单,可以进行二次开发。实现更好用的 功能。
python 多线程测试 源码python 多线程测试 源码
Python多线程超大日志文件解析转储,实现几十G超大文件并发处理。 实现功能如下: 1.多线程分块解析某超大日志文件,实现超大文件多线程分块处理 2.多线程写入数据到数据库postgresql/mysql 3.线程之间通过队列queue...
使用python多线程异步提高模型部署到rk3588NPU使用率_python源码+项目使用说明.zip 【项目资源说明】 使用多线程异步操作rknn模型, 提高rk3588/rk3588s的NPU使用率, 进而提高推理帧数(rk3568之类修改后应该也能使用,...
经常会遇到下述问题:很多io busy的应用采取多线程的方式来解决,但这时候会发现python命令行不响应ctrl-c 了,而对应的java代码则没有问题: 复制代码 代码如下: public class Test { public static void main...
能根据url的个数快速开启对个线程,单个线程可以实现对同一个url的多次访问,返回访问成功或者失败的结果
python多线程端口扫描工具,多线程自定义扫描主机开放端口。 Usage: portScan.py [options] Options: -h, --help show this help message and exit -i IP, --ip=IP scan only ip default port in number 1 ...
Python应用实战:python多线程-多线程安全问题&lock与rlock.zip