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