# 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 线程池有关的的更多相关文章

  1. 深入浅出 Java Concurrency (33): 线程池 part 6 线程池的实现及原理 (1)[转]

    线程池数据结构与线程构造方法 由于已经看到了ThreadPoolExecutor的源码,因此很容易就看到了ThreadPoolExecutor线程池的数据结构.图1描述了这种数据结构. 图1 Thre ...

  2. SpringBoot普通消息队列线程池配置

    1 package com.liuhuan.study.config; 2 3 import com.google.common.util.concurrent.ThreadFactoryBuilde ...

  3. NGINX引入线程池 性能提升9倍

    1. 引言 正如我们所知,NGINX采用了异步.事件驱动的方法来处理连接.这种处理方式无需(像使用传统架构的服务器一样)为每个请求创建额外的专用进程或者线程,而是在一个工作进程中处理多个连接和请求.为 ...

  4. java多线程系类:JUC线程池:06之Callable和Future(转)

    概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...

  5. 【转】线程及同步的性能 - 线程池 / ThreadPoolExecutors / ForkJoinPool

    线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理来自 ...

  6. 并发包的线程池第一篇--ThreadPoolExecutor执行逻辑

    学习这个很长时间了一直没有去做个总结,现在大致总结一下并发包的线程池. 首先,任何代码都是解决问题的,线程池解决什么问题? 如果我们不用线程池,每次需要跑一个线程的时候自己new一个,会导致几个问题: ...

  7. 转:Java Web应用中调优线程池的重要性

    不论你是否关注,Java Web应用都或多或少的使用了线程池来处理请求.线程池的实现细节可能会被忽视,但是有关于线程池的使用和调优迟早是需要了解的.本文主要介绍Java线程池的使用和如何正确的配置线程 ...

  8. java多线程系类:JUC线程池:05之线程池原理(四)(转)

    概要 本章介绍线程池的拒绝策略.内容包括:拒绝策略介绍拒绝策略对比和示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3512947.html 拒绝策略 ...

  9. 突破python缺陷,实现几种自定义线程池 以及进程、线程、协程的介绍

    Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...

随机推荐

  1. fedora21 中lamp的搭建(测试没有问题)

    LAMP Stands for Linux,Apache,MySQL and PHP. Most of the websites works with the above combination. T ...

  2. Confluence 6 配置边栏

    如果你具有空间的管理员权限,你可以对空间的变量进行自定义,让你的空间具有自己的空间标识(logo),修改显示的继承关系和在空间中添加快捷方式以帮助用户在空间中进行快速导航. 希望开始配置空间边栏,选择 ...

  3. SVN的安装

    Svn服务器的安装和配置 注意,一定要切换到最高管理权限:  su root  通过这个命令就可以完成! 1.安装svn服务器端软件从镜像服务器或者YUM源下载安装SVN服务器软件:yum insta ...

  4. 【洛谷p1926】小书童——蚂蚁大战

    f(今天开学第一天) 小书童——蚂蚁大战[传送门] 洛谷算法标签: 这个题要用排序是真的很神奇: 首先我们来理解一下题意:首先蚂蚁们按血量接受打击[魔鬼操作],血量最少的蚂蚁要走到最前面,所以我们可以 ...

  5. 【实战问题】【1】@PostConstruct 服务启动后加载两次的问题

    @PostConstruct:在服务启动时触发操作(我是用来更新微信的access_token) 解决方法: tomcat文件夹→conf→server.xml→将appBase="weba ...

  6. Python 3.X 要使用urllib.request 来抓取网络资源。转

    Python 3.X 要使用urllib.request 来抓取网络资源. 最简单的方式: #coding=utf-8 import urllib.request response = urllib. ...

  7. oracle的 表、 procedure、package等对象被锁,处理方法

    1.0  oracle中表被锁,处理方法 select t4.object_name, t3.spid, t1.oracle_username, t1.os_user_name  from v$pro ...

  8. 1004. Max Consecutive Ones III最大连续1的个数 III

    网址:https://leetcode.com/problems/max-consecutive-ones-iii/ 参考:https://leetcode.com/problems/max-cons ...

  9. MongoDB,无模式文档型数据库简介

    MongoDB的名字源自一个形容词humongous(巨大无比的),在向上扩展和快速处理大数据量方面,它会损失一些精度,在旧金山举行的MondoDB大会上,Merriman说:“你不适宜用它来处理复杂 ...

  10. linux使用lvresize和resize2fs调整lv大小

    以下操作基于场景:有两个同vg的lv(applv和rootlv),我们需要从applv腾出1G给rootlv. 1.缩小applv磁盘 lvresize -L -1G /dev/mapper/myvg ...