day 33 线程池有关的
# cpu 的核心数
# import os
# print(os.cpu_count()) #
# 爬虫的进程和线程的应用
# 第一步 虚拟一个浏览器下载 在cmd 里输入 pip install requests 会下载并安装成功后
# requests 意思就下载网站的源代码 没有解析的功能
# import requests
# response = requests.get('https://www.python.org')
# # print(response) #
# # 结果:<Response [200]> 只是内容网站内容并不会给解析
# print(response.text) # 类型字符串 就是源代码
# # =================================
# 题目 解析网址的长度
# import requests
# from threading import current_thread # 什么意思
# urls = ['https://www.python.org',
# 'https://www.baidu.com',
# 'https://www.jd.com',
# 'https://www.tmall.com',]
# def get(url):
# print('%s GET %s'%(current_thread().getName(),url)) #谁在拿到谁
# ##### current_thread().gernName() 好像是
# response = requests.get(url)
# if response.status_code == 200: #固定的 下载200才算成功
# return {'url':url,'text':response.text}
### get(url) 是拿到网址 和内容 返回值给下边prase函数的上传参数 res
# def prase(res): #解析
# print('%s GET %s'%(current_thread().getName(),res['url']))
# # ##print('[%s] prase res [%s]'%(res['url'],res['text'])) # ##和下边的是一样的 这个暂时不用
# print('[%s] prase res [%s]'% (res['url'],len(res['text']))) #结果用长度替换字典的网址内容代码
##以上两行可以写成一行
# print('[%s] <%s> (%s)'%(current_thread().getName(),res['url'],len(res['text']))) # 方法一 普通
# for url in urls:
# res = get(url)
# prase(res)
# 结果: 通过循环正常 串着执行 打印出
# MainThread GET https://www.python.org
# MainThread GET https://www.python.org
# [https://www.python.org] prase res [48856]
# MainThread GET https://www.baidu.com
# MainThread GET https://www.baidu.com
# [https://www.baidu.com] prase res [2443]
# MainThread GET https://www.jd.com
# MainThread GET https://www.jd.com
# [https://www.jd.com] prase res [124541]
# MainThread GET https://www.tmall.com
# MainThread GET https://www.tmall.com
# [https://www.tmall.com] prase res [212078]
#
# ------------------------------
#''
# 异步调用:
# 提交完任务(为该任务绑定一个回调函数),不用再原地等任务执行完毕拿到结果,可以直接提交下一个任务
# 一个任务一旦执行完毕就会自动触发回调函数的运行
#
# 回调函数的参数是单一的:
# 回调函数的参数就是它所绑定任务的返回值
# 进程的用法
import requests
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
from threading import current_thread
import time,os
def get(url):
print('%s GET %s' %(os.getpid(),url))
response=requests.get(url)
time.sleep(3)
if response.status_code == 200:
return {'url':url,'text':response.text}
def parse(obj):
res=obj.result()
print('[%s] <%s> (%s)' % (os.getpid(), res['url'],len(res['text'])))
if __name__ == '__main__':
urls = [
'https://www.python.org',
'https://www.baidu.com',
'https://www.jd.com',
'https://www.tmall.com', ]
t=ProcessPoolExecutor(2)
for url in urls:
t.submit(get,url).add_done_callback(parse)
#add_done_callback(parse) 相当于一个按钮 前面对象结束后自动触发
# 作用 增加一个回调函数
t.shutdown(wait=True)
print('主',os.getpid()) 线程的应用
import requests
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
from threading import current_thread
import time
import os
def get(url):
print('%s GET %s' %(current_thread().getName(),url))
response=requests.get(url)
time.sleep(3)
if response.status_code == 200:
return {'url':url,'text':response.text}
def parse(obj):
res=obj.result()
print('[%s] <%s> (%s)' % (current_thread().getName(), res['url'],len(res['text'])))
if __name__ == '__main__':
urls = [
'https://www.python.org',
'https://www.baidu.com',
'https://www.jd.com',
'https://www.tmall.com',
]
t=ThreadPoolExecutor(2)
for url in urls:
# obj = t.submit(get,url)
# obj.result()
t.submit(get,url).add_done_callback(parse)
t.shutdown(wait=True)
print('主',os.getpid())
day 33 线程池有关的的更多相关文章
- 深入浅出 Java Concurrency (33): 线程池 part 6 线程池的实现及原理 (1)[转]
线程池数据结构与线程构造方法 由于已经看到了ThreadPoolExecutor的源码,因此很容易就看到了ThreadPoolExecutor线程池的数据结构.图1描述了这种数据结构. 图1 Thre ...
- SpringBoot普通消息队列线程池配置
1 package com.liuhuan.study.config; 2 3 import com.google.common.util.concurrent.ThreadFactoryBuilde ...
- NGINX引入线程池 性能提升9倍
1. 引言 正如我们所知,NGINX采用了异步.事件驱动的方法来处理连接.这种处理方式无需(像使用传统架构的服务器一样)为每个请求创建额外的专用进程或者线程,而是在一个工作进程中处理多个连接和请求.为 ...
- java多线程系类:JUC线程池:06之Callable和Future(转)
概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...
- 【转】线程及同步的性能 - 线程池 / ThreadPoolExecutors / ForkJoinPool
线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理来自 ...
- 并发包的线程池第一篇--ThreadPoolExecutor执行逻辑
学习这个很长时间了一直没有去做个总结,现在大致总结一下并发包的线程池. 首先,任何代码都是解决问题的,线程池解决什么问题? 如果我们不用线程池,每次需要跑一个线程的时候自己new一个,会导致几个问题: ...
- 转:Java Web应用中调优线程池的重要性
不论你是否关注,Java Web应用都或多或少的使用了线程池来处理请求.线程池的实现细节可能会被忽视,但是有关于线程池的使用和调优迟早是需要了解的.本文主要介绍Java线程池的使用和如何正确的配置线程 ...
- java多线程系类:JUC线程池:05之线程池原理(四)(转)
概要 本章介绍线程池的拒绝策略.内容包括:拒绝策略介绍拒绝策略对比和示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3512947.html 拒绝策略 ...
- 突破python缺陷,实现几种自定义线程池 以及进程、线程、协程的介绍
Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...
随机推荐
- MyEclipse6.5的SVN插件的安装
在线安装 1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→Find and Install; 2. 选择Search for new features to i ...
- 基于react的记账簿开发
前言 前端是纯 React,后端通过 axios 库请求服务器获得数据. 源码: https://github.com/hfpp2012/react-accounts-app 项目详解: https: ...
- 04 爬虫数据存储之Mongodb
MongoDB 认识MongoDB MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB是一个介于关系数据库和非关系数据 ...
- SP10707 COT2 - Count on a tree II (树上莫队)
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...
- Symmetric Tree leetcode java
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- Matlab-10:Ritz-Galerkin方法求解二阶常微分方程
一.代数多项式法: tic; clear clc % N=input('please key in the value of ''N'''); N=10; M=100; h=1/M; X=0:h:1; ...
- java中堆与栈的区别
堆与栈都是java中常用的存储结构,是内存中存放数据的地方. 堆:主要存放运行时创建(new)的对象.主要用于储存对象,存取速度慢,可以运行时动态分配内存,生命周期不需要提前确定. 栈:主要存放基础类 ...
- hdu 1025LIS思路同1257 二分求LIS
题目: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- [洛谷 P2709] 小B的询问
P2709 小B的询问 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数 ...
- 设计模式之单例模式-C++
单例模式也称单子模式.单件模式,通过单例模式可以保证系统中只有一个类只有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享 对于系统中的某些类来说,只有一个实例很重要,比如一个打印机可以 ...