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

#线程间的通信

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. 初识容器和Docker

    什么是Docker? Docker 是一个用于开发,交付和运行应用程序的开放平台.能够就应用程序和基础架构分开,从而可以快速的交付软件. 借助Docker可以和管理应用程序的方式来管理基础架构. 使用 ...

  2. react -搭建服务-2

    export const DEFAULT_TITLE = "你好"; // export const PRODUCT_SERVER_URL = "http://10.10 ...

  3. Python之基于十六进制判断文件类型

    核心代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : suk import struct from io import Byt ...

  4. Linux 系统中 grep 的ABC参数含义

    1.grep  -A  5   匹配行及后5行 2.grep  -B  5   匹配行及前5行 3.grep  -C  5   匹配行及前后各5行

  5. [Linux系统] (5)系统网络

    一.路由表 路由表是如何决策的: [root@centos-clone1 ~]# route -n Kernel IP routing table Destination Gateway Genmas ...

  6. codevs 5929 亲戚x

                         题目描述 Description 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. ...

  7. JDK_API剖析之java.lang包

    java.lang是Java语言的基础包,默认包中的所有来自动import到你写的类中.(按照字母排序) 1.AbstractMethodError 类.错误 自1.0开始有 继承自Incompati ...

  8. ABP .net Core MQTT+signalr通讯

    abp版本: 4.3.0.0 .net core 版本 2.2 1.Mqtt 1.1 添加程序集:M2MqttDotnetCore(差点以为没有.net core 的) 2.2 实现代码:抄了个单例模 ...

  9. oracle ROW_NUMBER() OVER(PARTITION BY '分组' ORDER BY '排序' DESC) 用法

    转载:https://blog.csdn.net/dbagaoshou/article/details/51330829 SELECT * FROM ( SELECT ROW_NUMBER() OVE ...

  10. Jmeter连接Redis服务缓存

    1.添加线程组->Sampler->BeanShell Sampler,加入以下内容: import redis.clients.jedis.Jedis; import org.apach ...