python使用run_in_executor线程池结合requests.get函数下载,使用functools.partial偏函数赋值

functools.partial用来冻结函数的参数,类似js中的 function.bind函数绑定

例子:

# coding=utf-8
import functools

def deal(x, y, z):
    print("x = {}, y = {}, z = {}".format(x, y, z))

def callFunc():
    call = functools.partial(deal, x=8, y=9, z="hellkew")
    call()

if __name__ == "__main__":
    callFunc()

结果:

$ python my.py
x = 8, y = 9, z = hellkew

结合run_in_executor和requests.get,添加超时参数

#result = await loop.run_in_executor(gPool, requests.get, htmlUrl)
#改写成
callFunc = functools.partial(requests.get, url=htmlUrl, timeout=60)
result = await loop.run_in_executor(gPool, callFunc)

完整代码:

# coding=utf-8
import requests
import os
import sys
import chardet
import asyncio
import threading
import urllib
import functools
# from queue import Queue
from concurrent.futures import ThreadPoolExecutor
from enum import Enum
from html.parser import HTMLParser
gBook2Url = [
    ("娱乐至死", "https://www.sfs.com/mp3/11152.html"),
    # ("xxx", "xx"),
    # ("xxx", "xx"),
]

gPool = ThreadPoolExecutor(30)
# gHtmlPool = ThreadPoolExecutor(10)
gLock = threading.Lock()
gMp3Suffix = [".m4a", ".mp3"]


def getPath(book, fileName):
    return os.path.sep.join((book, fileName))


class EProcess(Enum):
    idle = 0
    start = 1
    end = 2


class CParseHtml(HTMLParser):

    def __init__(self, book, url):
        HTMLParser.__init__(self)
        hName = os.path.basename(url)
        prefix = hName.strip(".html").strip("?")
        self.mlog = "log" + prefix + ".txt"
        self.mBook = book
        if os.path.isfile(getPath(book, self.mlog)):
            os.remove(getPath(book, self.mlog))
        self.mUrls = []
        self.mProcess = EProcess.idle
   
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值