在多线程中,数据是共享,如何在多线程安全的通信,是首先要可虑的问题的

#线程间的通信

import time
import threading
from threading import RLock detail_url_list = [] lock = RLock() def get_detail_html(url):
#爬取文章详情页
global detail_url_list
#第一次我的想法也是用for循环,
# 但是你要知道,爬取文章的列表页要快于爬取文章详情页
#所以开启多个线程来爬取多个文章详情页
lock.acquire()
url = detail_url_list.pop()
print('get detail html started')
time.sleep(2)
print('get detail html end')
lock.release()
'''
for url in detail_url_list:
print('get detail html started')
time.sleep(2)
print('get detail html end')
''' def get_detail_url(url):
#爬取文章列表页
global detail_url_list
print('get detail url started')
time.sleep(4)
for i in range(20):
detail_url_list.append('http://projectsedu.com/{id}'.format(id=i))
print('get detail url end') #需求就是爬取文章列表页的url给文章详情页的url爬取:
#这个时候,设计到文章间的资源通信 #第一种方法就是 共享变量(共享变量其实就是全局变量,给各个函数调用)
#具体方法如下: if __name__ == '__main__':
# thread1 = threading.Thread(target=get_detail_html,args=(('',)))
for i in range(10):
thread1 = threading.Thread(target=get_detail_html)
thread1.start()
thread2 = threading.Thread(target=get_detail_url,args=(('http://bolezaixian.com',)))
thread2.start()
# start_time = time.time()
# thread1.setDaemon(True)#设置线程1为守护线程
# thread1.start()
# thread2.start()
# thread2.join()
# print('last time:{}'.format(time.time()-start_time))
共享变量也是要枷锁的。
import threading
from threading import Lock
#把共享变量存在settings配置文件中
import settings
import time lock = Lock() def get_detail_html():
#爬取文章详情页 detail_url_list=settings.detail_list_url
#第一次我的想法也是用for循环,
# 但是你要知道,爬取文章的列表页要快于爬取文章详情页
#所以开启多个线程来爬取多个文章详情页
while True:
try:
if len(detail_url_list):
# lock.acquire()
url = detail_url_list.pop()
print('get detail html started')
time.sleep(2)
print('get detail html end')
# lock.release()
except Exception as e:
print(e)
print('线程已运行完了')
break
'''
for url in detail_url_list:
print('get detail html started')
time.sleep(2)
print('get detail html end')
''' def get_detail_url():
#爬取文章列表页 detail_url_list = settings.detail_list_url
print('get detail url started')
time.sleep(4)
for i in range(20):
detail_url_list.append('http://projectsedu.com/{id}'.format(id=i))
print('get detail url end') if __name__ == '__main__':
start_time = time.time()
for i in range(10):
t = threading.Thread(target=get_detail_html)
t.start() t1 = threading.Thread(target=get_detail_url)
t1.start()
t1.join() print('total_time:{}'.format(time.time()-start_time))
#通过queue的方式进行线程间同步通信

-----------------------------------------------------------------------------------------------------------------

from queue import Queue

import time
import threading def get_detail_html(queue):
#爬取文章详情页
while True:
url = queue.get() #get()方法是一个阻塞的方法,如果queue是空队列,它一直会阻塞在这 print('get detail html started')
time.sleep(2)
print('get detail html end') def get_detail_url(queue):
#爬取文章列表页 while True:
print('get detail url started')
time.sleep(2)
for i in range(20):
queue.put("https://projectsedu.com/{id}".format(id=i))
print('get detail url end') if __name__ == "__main__":
detail_url_queue = Queue(maxsize=1000)#队列里面一定要设置下,maxsize的最大值,防止内存过大 thread_detail_url = threading.Thread(target=get_detail_url,args=((detail_url_queue,))) for i in range(10):
html_thread = threading.Thread(target=get_detail_html,args=((detail_url_queue,)))
html_thread.start() detail_url_queue.task_done()
#队列调用join()方法阻塞在这,只有调用task_done()方法队列才结束,主线程才能运行。
detail_url_queue.join() qsize()方法判断队列的大小,empty()方法判断队列是否为空,如果为空,get()是会阻塞在哪,full()方法判断队列是否已满,如果以满,put()方法是会阻塞在哪的

线程间通信共享变量和queue的更多相关文章

  1. python 线程间通信之Condition, Queue

    Event 和 Condition 是threading模块原生提供的模块,原理简单,功能单一,它能发送 True 和 False 的指令,所以只能适用于某些简单的场景中. 而Queue则是比较高级的 ...

  2. 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题

    调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...

  3. volatile关键字与线程间通信

    >>Java内存模型 现在计算机普遍使用多处理器进行运算,并且为了解决计算机存储设备和处理器的运算速度之间巨大的差距,引入了高速缓存作为缓冲,缓存虽然能极大的提高性能,但是随之带来的缓存一 ...

  4. 线程间通信的三种方式(NSThread,GCD,NSOperation)

    一.NSThread线程间通信 #import "ViewController.h" @interface ViewController ()<UIScrollViewDel ...

  5. 源码分析Android Handler是如何实现线程间通信的

    源码分析Android Handler是如何实现线程间通信的 Handler作为Android消息通信的基础,它的使用是每一个开发者都必须掌握的.开发者从一开始就被告知必须在主线程中进行UI操作.但H ...

  6. 如何使用 volatile, synchronized, final 进行线程间通信

    原文地址:https://segmentfault.com/a/1190000004487149.感谢作者的无私分享. 你是否真正理解并会用volatile, synchronized, final进 ...

  7. Android线程间通信机制——深入理解 Looper、Handler、Message

    在Android中,经常使用Handler来实现线程间通信,必然要理解Looper , Handler , Message和MessageQueue的使用和原理,下面说一下Looper , Handl ...

  8. Java多线程:线程间通信之volatile与sychronized

    由前文Java内存模型我们熟悉了Java的内存工作模式和线程间的交互规范,本篇从应用层面讲解Java线程间通信. Java为线程间通信提供了三个相关的关键字volatile, synchronized ...

  9. Android中线程间通信原理分析:Looper,MessageQueue,Handler

    自问自答的两个问题 在我们去讨论Handler,Looper,MessageQueue的关系之前,我们需要先问两个问题: 1.这一套东西搞出来是为了解决什么问题呢? 2.如果让我们来解决这个问题该怎么 ...

随机推荐

  1. Qt5多线程

    方法一:重写run函数 一.线程执行过程: 1.在工程中自定义一个类mythread继承与QThread: 2.重写run函数: 3.创建线程对象: 4.写按钮槽函数: ui界面加入一个按钮 给按钮添 ...

  2. CSS定位——浮动定位

    CSS定位机制Ⅱ——浮动定位 float属性:进行浮动定位   left,right clear属性:清除浮动   left,right,both  ㈠  float属性 1.概述 ⑴div实现横向多 ...

  3. javascript中继承方式及优缺点(一)

    分别介绍原型链继承.call/apply继承(借用构造函数继承).组合继承.原型式继承.寄生式继承.寄生组合式继承 1. 原型链继承 核心:将父类的实例作为子类的原型 function SuperTy ...

  4. Confluence 6 预览一个文件

    当你浏览一个页面的时候,单击一个图片,文件缩略图或者链接将会运行预览. 预览视图包括了从远程 Web 页面导入的图片文件和已经附加到页面中的文件(尽管有可能这些文件没有在页面中显示). 在预览中你可以 ...

  5. matplotlib中plt.scatter()参数详解

    scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, l ...

  6. python爬取智联招聘职位信息(单进程)

    我们先通过百度搜索智联招聘,进入智联招聘官网,一看,傻眼了,需要登录才能查看招聘信息 没办法,用账号登录进去,登录后的网页如下: 输入职位名称点击搜索,显示如下网页: 把这个URL:https://s ...

  7. JS基础-连续赋值

    重点:先声明,从左向右,声明变量分配内存,后赋值,从右向左 问题 var a = {n: 1}; var b = a; a.x = a = {n: 2}; console.log(a.x); cons ...

  8. Redis Cluster with SpringBoot

    前提: 按照 https://www.cnblogs.com/luffystory/p/12081074.html 配置好Redis Cluster in Ubuntu 按照如下结构搭建项目结构: P ...

  9. 网页中JS函数自动执行常用三种方法

    (1)最简单的调用方式,直接写到html的body标签里面:        <body onload="myFunction()"></body>      ...

  10. LeetCode 47. 全排列 II(Permutations II)

    题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode4 ...