python 进程/线程/协程 测试
# Author: yeshengbao
# -- coding: utf-8 --
# @Time : 2018/5/24 21:38
# 进程:如一个人拥有分身(分数数最好为cpu核心数)几乎同时进行做工
# 线程:如这个人正在烧开水,但同时又可以在烧水时间内去吃饭,和扫地,这时线程就会对其随机选择,可能还会出现地还没扫完,水就开了,但他还会扫地{这就可能出现数据丢失}。。
# 协程:这个一个比线程更小的线程非常相似,但他在执行任务时,已经被规划好了,不会就行额外的时间浪费,创建时更省资源 import datetime
import requests
import os
import hashlib
from multiprocessing import Process
from lxml import etree
from threading import Thread
from gevent import monkey
import gevent monkey.patch_socket() # 开启猴子方法, 必须加
bag = '書'
if not os.path.exists(bag):
os.mkdir(bag) class DouTu(object):
def __init__(self):
self.url = 'http://www.23us.so/files/article/html/6/6926/index.html'
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
" Chrome/64.0.3282.186 Safari/537.36",
} def md5(self, strs):
stri = hashlib.md5(strs.encode('utf-8'))
key = stri.hexdigest()
return key def get_source(self, url, headers):
try:
response = requests.get(url, headers=headers, timeout=10).content
return response
except Exception:
return self.get_source(url, headers) def get_detail_content(self, frction_detail_url):
if frction_detail_url:
html = self.get_source(frction_detail_url, self.headers).decode('utf-8')
doc = etree.HTML(html)
title = doc.xpath('.//div[@class="bdsub"]/dl/dd[1]/h1/text()')[0]
content = ''.join(doc.xpath('.//div[@class="bdsub"]/dl/dd[@id="contents"]/text()')).strip().replace('\n', '').replace('\t', '')
if content:
with open(bag + '\\' + 'text.txt', 'a+' , encoding='utf-8')as fp:
fp.write(title + ' :' + content + '\n')
print('正在写入{}_{}'.format(title, content)) def analysis_index(self, html):
doc = etree.HTML(html)
td_list = doc.xpath('.//table[@id="at"]//td[@class="L"]')
thread_list = []
for td in td_list:
xie = gevent.spawn(self.get_detail_content, td.xpath('./a/@href')[0])
xie.start()
thread_list.append(xie)
print(xie) # while True:
# if len(thread_list) < 100: # 可选择开启多少线程
# th = Thread(target=self.get_detail_content, args=(td.xpath('./a/@href')[0], ))
# th.start()
# thread_list.append(th)
# break
#
# else:
# print(thread_list)
# #time.sleep(3)
# for ths in thread_list:
# if not ths.is_alive():
# thread_list.remove(ths) for th in thread_list: # 为保证线程或协程的运行结束
th.join() def begin_spider(self):
html = self.get_source(self.url, self.headers).decode('utf-8')
self.analysis_index(html) start_time = datetime.datetime.now() # 程序开始时间
doutu = DouTu()
doutu.begin_spider()
over_time = datetime.datetime.now() # 程序结束时间
total_time = (over_time-start_time).total_seconds()
print('程序共计%s秒' % total_time) # 线程 620页 约40s
# 协程 18s # thread_lists = []
# for page in range(50, 81):
# while True:
# if len(thread_lists) < 8:
# # th = threading.Thread(target=dou.begin_by_page,args=(page,))
# th = multiprocessing.Process(target=dou.begin_by_page, args=(page,))
# th.start()
# thread_lists.append(th)
# break
# else:
# time.sleep(3)
# print(thread_lists)
# print('进程池已经满了')
# for ths in thread_lists:
# if not ths.is_alive():
# thread_lists.remove(ths)
# for ths in thread_lists:
# ths.join()
python 进程/线程/协程 测试的更多相关文章
- Python 进程线程协程 GIL 闭包 与高阶函数(五)
Python 进程线程协程 GIL 闭包 与高阶函数(五) 1 GIL线程全局锁 线程全局锁(Global Interpreter Lock),即Python为了保证线程安全而采取的独立线程运行的 ...
- python -- 进程线程协程专题
进程专栏 multiprocessing 高级模块 要让Python程序实现多进程(multiprocessing),我们先了解操作系统的相关知识. Unix/Linux操作系统提供了一个fork() ...
- python 进程 线程 协程
并发与并行:并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔内发生.在单核CPU下的多线程其实都只是并发,不是并行. 进程是系统资源分配的最小单位,进程的出现是为了更好的 ...
- python进程/线程/协程
一 背景知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所 ...
- Python并发编程系列之常用概念剖析:并行 串行 并发 同步 异步 阻塞 非阻塞 进程 线程 协程
1 引言 并发.并行.串行.同步.异步.阻塞.非阻塞.进程.线程.协程是并发编程中的常见概念,相似却也有却不尽相同,令人头痛,这一篇博文中我们来区分一下这些概念. 2 并发与并行 在解释并发与并行之前 ...
- python自动化开发学习 进程, 线程, 协程
python自动化开发学习 进程, 线程, 协程 前言 在过去单核CPU也可以执行多任务,操作系统轮流让各个任务交替执行,任务1执行0.01秒,切换任务2,任务2执行0.01秒,在切换到任务3,这 ...
- 进程&线程&协程
进程 一.基本概念 进程是系统资源分配的最小单位, 程序隔离的边界系统由一个个进程(程序)组成.一般情况下,包括文本区域(text region).数据区域(data region)和堆栈(stac ...
- 多道技术 进程 线程 协程 GIL锁 同步异步 高并发的解决方案 生产者消费者模型
本文基本内容 多道技术 进程 线程 协程 并发 多线程 多进程 线程池 进程池 GIL锁 互斥锁 网络IO 同步 异步等 实现高并发的几种方式 协程:单线程实现并发 一 多道技术 产生背景 所有程序串 ...
- python的进程/线程/协程
1.python的多线程 多线程就是在同一时刻执行多个不同的程序,然而python中的多线程并不能真正的实现并行,这是由于cpython解释器中的GIL(全局解释器锁)捣的鬼,这把锁保证了同一时刻只有 ...
随机推荐
- springboot+ibatis 多数据源配置
这个是boot基本版本包,因为我用的打包方式是war所以去除掉了boot内置的tomcat,但是为了方便测试又引入了内置tomcat,只要添加<scope>provided</sco ...
- 常见的CSS
/***** Selector Hacks ******/ /* IE6 and below */ * html #uno { color: red } /* IE7 */ *:first-child ...
- C#经典面试题及答案
1.请你说说.net 中类和结构的区别? 答:结构和类具有大体的语法,但是结构受到的限制比类多.结构不能声明默认的的构造函数,为结构的副本是编译器创建和销毁的,所以不需要默认的构造函数和析构函数,结构 ...
- 【TLV】非递归TLV数据解析
#include <stdio.h> #define X_LEN_OF_TAG_MAX ( 2 ) #define X_LEN_OF_LEN_MAX ( 2 ) struct st_tlv ...
- Python全栈之路----常用模块----re 模块
正则表达式就是字符串的匹配规则,在多数编程语言里都有相应的支持,python里对应的模块是 re. re的匹配语法有以下几种 re.match 从头开始匹配 re.search 匹配包含 re.fin ...
- 锚点的animate使用过程中定位不准确的问题小记
源码: $('html, body, .S').animate({ scrollTop: $('.a1').offset().top - 133}, { duration: 1500, easing: ...
- 关于 API
1. API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访 ...
- docker hub切换国内镜像
官方docker hub访问非常的慢,安装之后最好先切换国内镜像: 执行: curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh ...
- 学习笔记TF065:TensorFlowOnSpark
Hadoop生态大数据系统分为Yam. HDFS.MapReduce计算框架.TensorFlow分布式相当于MapReduce计算框架,Kubernetes相当于Yam调度系统.TensorFlow ...
- java基本数据类型和运算符
一.基本数据类型 种类: 内置数据类型 引用数据类型 1.内置数据类型 一共有八种基本类型,六个数字类型(四个整数类型,两个浮点型),一个布尔型,一个字符类型. (1)byte: byte数据类型是8 ...