Python 线程的使用

Python里面的并发分2大类,cpu密集和IO密集,也是面试中经常考的!
多进程,多线程,协程!围绕的库大概有6-7种,如果深入研究会设计到Py3里面最核心的也是最难懂的asyncio库

在这里插入图片描述

import requests
import re
from concurrent.futures import ThreadPoolExecutor
import time

def download_html(word):
    output=[]
    headers = {
               'User-Agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 72.0.3626.121Safari / 537.36'
                 }
    url='http://dict.youdao.com/w/eng/{}/'.format(word)
    try:
        r=requests.get(url,headers=headers)
        if r.status_code==200:
            pattern=re.compile(' <span class="keyword">(.*?)</span>.*?<span class="pronounce">(.*?)\n.*?<span class="phonetic">(.*?)</span>.*?<span class="pronounce">(.*?)\n.*?<span class="phonetic">(.*?)</span>.*?<div class="trans-container">.*?<ul>.*?<li>(.*?)</li>.*?<li>(.*?)</li>',re.S)
            word=re.findall(pattern,r.text)
            print(word)
            output.append(word)
    except Exception as e:
        pass

    return output

if __name__ == '__main__':
    starttime=time.time()
    words = ['chain'] * 20
    pool =ThreadPoolExecutor(4)
    threads = [pool.submit(download_html(word)) for word in words]
    for i in threads:
        print(i.result)
    endtime=time.time()
    print(endtime-starttime)

结果:

[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
[('chain', '英', '[tʃeɪn]', '美', '[tʃen]', 'n. 链;束缚;枷锁', 'vt. 束缚;囚禁;用铁链锁住')]
<bound method Future.result of <Future at 0x1ce0b705278 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c64f668 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6579e8 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c657898 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c66aba8 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c66aa58 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c66a6a0 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c66ada0 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c66a9b0 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c66abe0 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c66a5c0 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6a70b8 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6a7828 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6a7320 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6a74a8 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6a7780 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6a7e10 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6a79e8 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6a7240 state=finished raised TypeError>>
<bound method Future.result of <Future at 0x1ce0c6a7be0 state=finished raised TypeError>>
4.2165422439575195

1 介绍
concurrent.futures模块提供了高度封装的异步调用接口
ThreadPoolExecutor:线程池,提供异步调用
ProcessPoolExecutor: 进程池,提供异步调用
Both implement the same interface, which is defined by the abstract Executor class.

2 基本方法
●submit(fn, *args, **kwargs)
异步提交任务

●map(func, *iterables, timeout=None, chunksize=1)
取代for循环submit的操作

●shutdown(wait=True)
相当于进程池的pool.close()+pool.join()操作
wait=True,等待池内所有任务执行完毕回收完资源后才继续
wait=False,立即返回,并不会等待池内的任务执行完毕
但不管wait参数为何值,整个程序都会等到所有任务执行完毕
submit和map必须在shutdown之前

●result(timeout=None)
取得结果

●add_done_callback(fn)
回调函数

详细https://blog.csdn.net/qq_41922768/article/details/84134852

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值