设为首页 加入收藏

TOP

Python核心技术与实战 笔记(四)
2019-09-20 11:45:42 】 浏览:170
Tags:Python 核心 技术 实战 笔记
cel() consumer_2.cancel() await asyncio.gather(consumer_1, consumer_2, producer_1, producer_2, return_exceptions=True) %time asyncio.run(main()) ########## 输出 ########## producer_1 put a val: 5 producer_2 put a val: 3 consumer_1 get a val: 5 consumer_2 get a val: 3 producer_1 put a val: 1 producer_2 put a val: 3 consumer_2 get a val: 1 consumer_1 get a val: 3 producer_1 put a val: 6 producer_2 put a val: 10 consumer_1 get a val: 6 consumer_2 get a val: 10 producer_1 put a val: 4 producer_2 put a val: 5 consumer_2 get a val: 4 consumer_1 get a val: 5 producer_1 put a val: 2 producer_2 put a val: 8 consumer_1 get a val: 2 consumer_2 get a val: 8 Wall time: 10 s

并发编程之 Futures

区别并发和并行

  • 并发通常应用与 I/O 操作频繁的场景,比如你要从网站上下载多个文件, I/O 操作的时间可能比 CPU 运行处理的时间长得多,通过线程和任务之间互相切换的方式实现,但同一时刻,只允许有一个线程或任务执行
  • 并行更多应用于 CPU heavy 的场景,比如 MapReduce 中的并行计算,为了加快运算速度,一般会用多台机器,多个处理器来完成。可以让多个进程完全同步同时的执行

Python 中之所以同一时刻只运行一个线程运行,其实是由于全局解释锁的存在。但对 I/O 操作而言,当其被 block 的时候,全局解释器锁便会被释放,使气体线程继续执行。

import concurrent.futures
import requests
import threading
import time

def download_one(url):
    resp = requests.get(url)
    print('Read {} from {}'.format(len(resp.content), url))

# 版本 1
def download_all(sites):
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        executor.map(download_one, sites)

# 版本 2
def download_all(sites):
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        to_do = []
        for site in sites:
            future = executor.submit(download_one, site)
            to_do.append(future)
            
        for future in concurrent.futures.as_completed(to_do):
            future.result()

def main():
    sites = [
        'https://en.wikipedia.org/wiki/Portal:Arts',
        'https://en.wikipedia.org/wiki/Portal:History',
        'https://en.wikipedia.org/wiki/Portal:Society',
        'https://en.wikipedia.org/wiki/Portal:Biography',
        'https://en.wikipedia.org/wiki/Portal:Mathematics',
        'https://en.wikipedia.org/wiki/Portal:Technology',
        'https://en.wikipedia.org/wiki/Portal:Geography',
        'https://en.wikipedia.org/wiki/Portal:Science',
        'https://en.wikipedia.org/wiki/Computer_science',
        'https://en.wikipedia.org/wiki/Python_(programming_language)',
        'https://en.wikipedia.org/wiki/Java_(programming_language)',
        'https://en.wikipedia.org/wiki/PHP',
        'https://en.wikipedia.org/wiki/Node.js',
        'https://en.wikipedia.org/wiki/The_C_Programming_Language',
        'https://en.wikipedia.org/wiki/Go_(programming_language)'
    ]
    start_time = time.perf_counter()
    download_all(sites)
    end_time = time.perf_counter()
    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))

if __name__ == '__main__':
    main()

## 输出
Read 151021 from https://en.wikipedia.org/wiki/Portal:Mathematics
Read 129886 from https://en.wikipedia.org/wiki/Portal:Arts
Read 107637 from https://en.wikipedia.org/wiki/Portal:Biography
Read 224118 from https://en.wikipedia.org/wiki/Portal:Society
Read 184343 from https://en.wikipedia.org/wiki/Portal:History
Read 167923 from https://en.wikipedia.org/wiki/Portal:Geography
Read 157811 from https://en.wikipedia.org/wiki/Portal:Technology
Read 91533 from https://en.wikipedia.org/wiki/Portal:Science
Read 321352 from https://en.wikipedia.org/wiki/Computer_science
Read 391905 from https://en.wikipedia.org/wiki/Python_(programming_la
首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇构建支持中文字体的moviepy镜像 下一篇django下创建多个app,如何设置每..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目