本文整理匯總了Python中utils.ThreadPool.ThreadPool.run方法的典型用法代碼示例。如果您正苦於以下問題:Python ThreadPool.run方法的具體用法?Python ThreadPool.run怎麽用?Python ThreadPool.run使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類utils.ThreadPool.ThreadPool
的用法示例。
在下文中一共展示了ThreadPool.run方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ProxySpider
# 需要導入模塊: from utils.ThreadPool import ThreadPool [as 別名]
# 或者: from utils.ThreadPool.ThreadPool import run [as 別名]
class ProxySpider(object):
def __init__(self, output_file=True, output_db=True, output_filename="proxy-ip-list.csv"):
# 初始化AutoLoad模塊
self.al = AutoLoad()
# 初始化
self.tp = None
self.sd = None
self.write_file_tp = None
self.spider_threads = None
self.save_data_threads = None
# 獲取參數
self.output_file = output_file
self.output_db = output_db
self.output_filename = output_filename
def load(self, *spiders):
self.al.load(*spiders)
def set_threads(self, spider_threads=0, save_data_threads=0):
if spider_threads > 0:
self.spider_threads = spider_threads
if save_data_threads > 0:
self.save_data_threads = save_data_threads
def start(self):
if not len(self.al.spiders):
logger.error("No Spiders loaded. exit.")
sys.exit(1)
else:
message = "Loaded spiders: "
for s in self.al.spiders:
message += str(s.__class__).split(".")[-1].split("'")[0] + ", "
logger.info(message.strip(", "))
# 創建線程池
if self.spider_threads:
self.tp = ThreadPool(self.spider_threads)
else:
self.tp = ThreadPool()
for sp in self.al.spiders:
# 將spider中的run方法添加到線程池中
self.tp.add_function(sp.run)
# 開始線程池
self.tp.run(join=False)
# 輸出結果
self.sd = SaveData(self.al.results, self.tp, use_file=self.output_file, use_database=self.output_db,
filename=self.output_filename)
if self.save_data_threads:
self.write_file_tp = ThreadPool(self.save_data_threads)
else:
self.write_file_tp = ThreadPool()
self.write_file_tp = ThreadPool()
self.write_file_tp.add_function(self.sd.write)
self.write_file_tp.run()