第一段代码:

 __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 多线程知识的更多相关文章

  1. python多线程知识-实用实例

    python多线程使用场景:IO操作,不适合CPU密集操作型任务   1.多个线程内存共享 2.线程同时修改同一份数据需要加锁,mutex互斥锁 3.递归锁:多把锁,锁中有锁 4.python多线程, ...

  2. 【Python爬虫】入门知识

    爬虫基本知识 这阵子需要用爬虫做点事情,于是系统的学习了一下python爬虫,觉得还挺有意思的,比我想象中的能干更多的事情,这里记录下学习的经历. 网上有关爬虫的资料特别多,写的都挺复杂的,我这里不打 ...

  3. python多线程爬虫设计及实现示例

    爬虫的基本步骤分为:获取,解析,存储.假设这里获取和存储为io密集型(访问网络和数据存储),解析为cpu密集型.那么在设计多线程爬虫时主要有两种方案:第一种方案是一个线程完成三个步骤,然后运行多个线程 ...

  4. python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。

    本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...

  5. python多线程爬虫+批量下载斗图啦图片项目(关注、持续更新)

    python多线程爬虫项目() 爬取目标:斗图啦(起始url:http://www.doutula.com/photo/list/?page=1) 爬取内容:斗图啦全网图片 使用工具:requests ...

  6. python 爬虫与数据可视化--python基础知识

    摘要:偶然机会接触到python语音,感觉语法简单.功能强大,刚好朋友分享了一个网课<python 爬虫与数据可视化>,于是在工作与闲暇时间学习起来,并做如下课程笔记整理,整体大概分为4个 ...

  7. python爬虫之多线程、多进程+代码示例

    python爬虫之多线程.多进程 使用多进程.多线程编写爬虫的代码能有效的提高爬虫爬取目标网站的效率. 一.什么是进程和线程 引用廖雪峰的官方网站关于进程和线程的讲解: 进程:对于操作系统来说,一个任 ...

  8. 线程概念( 线程的特点,进程与线程的关系, 线程和python理论知识,线程的创建)

    参考博客: https://www.cnblogs.com/xiao987334176/p/9041318.html 线程概念的引入背景 进程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运 ...

  9. python 全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python 理论知识,线程的创建)

    昨日内容回顾 队列 队列 : 先进先出.数据进程安全 队列实现方式: 管道 + 锁 生产者消费者模型 : 解决数据供需不平衡 管道 双向通信 数据进程不安全 EOFError: 管道是由操作系统进行引 ...

随机推荐

  1. mvc BundleConfig实现对Css、Js压缩打包加载

    Bundle不是.net Framework框架中的一员,使用Bundle首先要先添加引用,如下: nuget包管理--程序包管理控制台--Install-Package Microsoft.AspN ...

  2. 用Photoshop制作一寸照片

    好了简单介绍一下自己如何制作一寸照片.   工具/原料   Photoshop CS4 更高版本也可以 方法/步骤   1 打开你要修改的照片 2 选择裁剪工具设置参数   选择最佳位置裁剪   选择 ...

  3. Item 8 覆盖equals时请遵守通用约定

    在覆盖equals方法的时候,你必须要遵守它的通用约定,不遵守,写出来的方法,会出现逻辑错误.下面是约定的内容:   equals方法实现了等价关系:   自反性.对于任何非null的引用值,x.eq ...

  4. HDU 2639 Bone Collector II (dp)

    题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...

  5. mybatis 插入语句name no find

    1.可参考连接:https://www.cnblogs.com/thomas12112406/p/6217211.html 2.dao层的配置 void addUser(@Param("un ...

  6. 中南oj String and Arrays

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2072&pid=1 Problem B: String and Arrays T ...

  7. jeecg3.7中弹出窗操作标签dgOpenOpt的用法

    1.基本参数 参数名                    描述 url                           弹出页面地址 title                         ...

  8. 线程,JSP,Servlet面试题

    线程编程方面 60.java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现Runna ...

  9. 比特币编译(Ubuntu 16.04)

    安装比特币需要的所有库 sudo apt-get install build-essential libtool autotools-dev automake pkg-config libssl-de ...

  10. WiderFace标注格式转PASCAL VOC2007标注格式

    #coding=utf-8 import os import cv2 from xml.dom.minidom import Document def create_xml(boxes_dict,ta ...