目的:多线程请求url, 提高qps
代码分享:
coding=utf-8
import os
import time
import threading
from random import random
from Queue import Queue
import sys, json, requests, datetime
from threading import Thread, Lock
import multiprocessing as mp
import logging
import logging.handlers
class LogLauncher:
@staticmethod
def launch(log_file_path, log_level):
dir_name = os.path.dirname(log_file_path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
level_dict = {“ERROR”: logging.ERROR, “WARNING”: logging.WARNING, “INFO”: logging.INFO, “DEBUG”: logging.DEBUG}
log_level = log_level.upper()
level_indicator = logging.DEBUG
if level_dict.has_key(log_level):
level_indicator = level_dict[log_level]
log = logging.getLogger(’’)
log.setLevel(level_indicator)
format = “%(asctime)s %(filename)s:%(lineno)d [%(levelname)s] %(

本文介绍了一种使用Python实现多线程工作队列来提高Url请求速度的方法。通过创建线程池,将请求任务放入队列,实现了并发发送Url请求,同时配合日志记录错误和状态。
最低0.47元/天 解锁文章
781





