好吧,大家让我举例说明,我有这个
def get_config_file(file='scrapers_conf.json'):
"""
Load the default .json config file
"""
return json.load(open(file))
这个函数被调用很多,这将在服务器上,每个请求都会触发这个函数至少 5 次,我有多个刮刀在运行,每个刮刀都在以下形状上。
为了方便起见,我删除了辅助方法,问题是,每个抓取工具都应该有自己的请求标头、有效负载……或者使用位于的默认标头scrapers_conf.json
class Scraper(threading.Thread): # init is overriden and has set .conf
def run(self):
self.get()
def get(self):
# logic
问题是我得到的标题像
class Scraper(threading.Thread):
def run(self):
self.get()
def get(self):
headers = self.conf.get('headers') or get_config_file().get('headers')
正如您所看到的,每个请求的每个实例都会调用 get_config_file() 函数,我认为这在我的情况下不是最佳的。我知道,lru_cache但我不认为这是最佳解决方案(请纠正我!)
配置文件很小,os.sys.getsizeof报告不到 1 KB。
我正在考虑将其保留,因为考虑到读取 1 KB 不是问题。
炎炎设计
梦里花落0921
相关分类