【爬虫】python 多线程知识
第一段代码:
__author__ = 'Administrator' import threading
import time index = 0 class MyThread(threading.Thread):
def __init__(self, threadname, counter):
super(MyThread, self).__init__()
self.threadname = threadname
self.counter = counter def run(self):
global index
print "starting " + self.threadname
while self.counter:
time.sleep(1)
index += 1
print "threadname: %s\tindex:%d\ttime: %s\n"%(self.threadname, index, time.ctime())
self.counter -= 1 def main():
print '=======================BEGIN==========================='
thread1 = MyThread('thread1', 10)
thread2 = MyThread('thread2', 5)
thread1.start()
thread2.start() thread1.join()
thread2.join() print '========================END============================' if __name__ == '__main__':
main()
第二段代码:
# -*- coding: gbk -*-
__author__ = 'Administrator' import threading
import random, time, Queue # 缓冲队列大小为5
MAX_SIZE = 5
# 模拟队列
SHARE_Q = []
# 条件变量
CONDITION = threading.Condition() class Producer(threading.Thread):
def run(self):
products = range(5)
global SHARE_Q
while True:
# 获得锁
CONDITION.acquire()
# 队列如果满了,则等待资源释放
if len(SHARE_Q) == 5:
print "Queue is full..."
CONDITION.wait()
print "Consumer have consumed something"
product = random.choice(products)
SHARE_Q.append(product)
print "Producer : ", product
CONDITION.notify()
CONDITION.release()
time.sleep(3) class Consumer(threading.Thread):
def run(self):
global SHARE_Q
while True:
CONDITION.acquire()
# 队列为空,消费者不能进行消费
if not SHARE_Q:
print "Queue is Empty..."
CONDITION.wait()
print "Producer have produced something"
product = SHARE_Q.pop(0)
print "Consumer: ", product
CONDITION.notify()
CONDITION.release()
time.sleep(2) def main():
producer = Producer()
consumer = Consumer()
producer.start()
consumer.start() if __name__ == '__main__':
main()
【爬虫】python 多线程知识的更多相关文章
- python多线程知识-实用实例
python多线程使用场景:IO操作,不适合CPU密集操作型任务 1.多个线程内存共享 2.线程同时修改同一份数据需要加锁,mutex互斥锁 3.递归锁:多把锁,锁中有锁 4.python多线程, ...
- 【Python爬虫】入门知识
爬虫基本知识 这阵子需要用爬虫做点事情,于是系统的学习了一下python爬虫,觉得还挺有意思的,比我想象中的能干更多的事情,这里记录下学习的经历. 网上有关爬虫的资料特别多,写的都挺复杂的,我这里不打 ...
- python多线程爬虫设计及实现示例
爬虫的基本步骤分为:获取,解析,存储.假设这里获取和存储为io密集型(访问网络和数据存储),解析为cpu密集型.那么在设计多线程爬虫时主要有两种方案:第一种方案是一个线程完成三个步骤,然后运行多个线程 ...
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...
- python多线程爬虫+批量下载斗图啦图片项目(关注、持续更新)
python多线程爬虫项目() 爬取目标:斗图啦(起始url:http://www.doutula.com/photo/list/?page=1) 爬取内容:斗图啦全网图片 使用工具:requests ...
- python 爬虫与数据可视化--python基础知识
摘要:偶然机会接触到python语音,感觉语法简单.功能强大,刚好朋友分享了一个网课<python 爬虫与数据可视化>,于是在工作与闲暇时间学习起来,并做如下课程笔记整理,整体大概分为4个 ...
- python爬虫之多线程、多进程+代码示例
python爬虫之多线程.多进程 使用多进程.多线程编写爬虫的代码能有效的提高爬虫爬取目标网站的效率. 一.什么是进程和线程 引用廖雪峰的官方网站关于进程和线程的讲解: 进程:对于操作系统来说,一个任 ...
- 线程概念( 线程的特点,进程与线程的关系, 线程和python理论知识,线程的创建)
参考博客: https://www.cnblogs.com/xiao987334176/p/9041318.html 线程概念的引入背景 进程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运 ...
- python 全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python 理论知识,线程的创建)
昨日内容回顾 队列 队列 : 先进先出.数据进程安全 队列实现方式: 管道 + 锁 生产者消费者模型 : 解决数据供需不平衡 管道 双向通信 数据进程不安全 EOFError: 管道是由操作系统进行引 ...
随机推荐
- struts2之OGNL用法
浅析OGNL OGNL是Object-GraphNavigation Language的缩写,是一种功能强大的表达式语言 通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对 ...
- RabbitMQ与Spring集成
RabbitMQ服务端安装: https://blog.csdn.net/hzw19920329/article/details/53156015 与Spring集成 https://www.cnbl ...
- 安装显卡后蓝屏0x00000116解决办法
1. 亲自遇到蓝屏的问题. 2. 进入安全模式后重装系统,成功. 3. 安装驱动,软件没毛病.安装显卡驱动后,蓝屏. 4. 查看蓝屏错误代码0x00000116, 百度后查看到是显卡驱动的问题. 5. ...
- hdu 2680 Choose the best route (dijkstra算法 最短路问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS ( ...
- 在使用ubuntu16.04+python3.5 下使用pip3出现pip3 error - '_NamespacePath' object has no attribute 'sort'
使用pip3安装tensorflow以及gensim等时,出现如下错误: Traceback (most recent call last): File "/usr/local/bin/pi ...
- js获取链接参数
var url = location.search; var Request = new Object(); if(url.indexOf("?")!=-1){ var str = ...
- vmware安装ubuntu " Intel VT-x 处于禁用状态"
vmware安装ubuntu " Intel VT-x 处于禁用状态" http://jingyan.baidu.com/article/fc07f98976710e12ffe51 ...
- python基础===利用PyCharm进行Python远程调试(转)
原文链接:利用PyCharm进行Python远程调试 背景描述 有时候Python应用的代码在本地开发环境运行十分正常,但是放到线上以后却出现了莫名其妙的异常,经过再三排查以后还是找不到问题原因,于是 ...
- 从LFS官方文档构建完整Linux系统
从LFS官方文档构建完整Linux系统 http://www.cnblogs.com/sonofdark/p/4962609.html 这不是新手教程!!! Parallels Desktop (为防 ...
- python爬虫模块之URL管理器模块
URL管理器模块 一般是用来维护爬取的url和未爬取的url已经新添加的url的,如果队列中已经存在了当前爬取的url了就不需要再重复爬取了,另外防止造成一个死循环.举个例子 我爬www.baidu. ...