class ServersConnectivityTester(object): """Utility class to run servers connectivity testing on a list of ServerConnectivityInfo using a thread pool. """ DEFAULT_MAX_THREADS = 50 def __init__(self, tentative_server_info_list): # Use a thread pool to connect to each server self._thread_pool = ThreadPool() self._server_info_list = tentative_server_info_list def start_connectivity_testing(self, max_threads=DEFAULT_MAX_THREADS, network_timeout=None): for tentative_server_info in self._server_info_list: self._thread_pool.add_job( (tentative_server_info.test_connectivity_to_server, [network_timeout])) nb_threads = min(len(self._server_info_list), max_threads) self._thread_pool.start(nb_threads) def get_reachable_servers(self): for (job, _) in self._thread_pool.get_result(): test_connectivity_to_server_method, _ = job server_info = test_connectivity_to_server_method.__self__ yield server_info def get_invalid_servers(self): for (job, exception) in self._thread_pool.get_error(): test_connectivity_to_server_method, _ = job server_info = test_connectivity_to_server_method.__self__ yield (server_info, exception)
class ServersConnectivityTester(object): """Utility class to run servers connectivity testing on a list of ServerConnectivityInfo using a thread pool. """ DEFAULT_MAX_THREADS = 50 def __init__(self, tentative_server_info_list): # Use a thread pool to connect to each server self._thread_pool = ThreadPool() self._server_info_list = tentative_server_info_list def start_connectivity_testing(self, max_threads=DEFAULT_MAX_THREADS, network_timeout=None): for tentative_server_info in self._server_info_list: self._thread_pool.add_job((tentative_server_info.test_connectivity_to_server, [network_timeout])) nb_threads = min(len(self._server_info_list), max_threads) self._thread_pool.start(nb_threads) def get_reachable_servers(self): for (job, _) in self._thread_pool.get_result(): test_connectivity_to_server_method, _ = job server_info = test_connectivity_to_server_method.__self__ yield server_info def get_invalid_servers(self): for (job, exception) in self._thread_pool.get_error(): test_connectivity_to_server_method, _ = job server_info = test_connectivity_to_server_method.__self__ yield (server_info, exception)
def __init__(self, tentative_server_info_list): # Use a thread pool to connect to each server self._thread_pool = ThreadPool() self._server_info_list = tentative_server_info_list
def __init__(self, tentative_server_info_list): # type: (List[ServerConnectivityInfo]) -> None # Use a thread pool to connect to each server self._thread_pool = ThreadPool() self._server_info_list = tentative_server_info_list
from __future__ import absolute_import # 日志配置 import logging.config from utils.log_handler import get_log_conf login_dict = get_log_conf() logging.config.dictConfig(login_dict) logger = logging.getLogger('web_info') logcelery = logging.getLogger('celery_info') # mysql配置 import pymysql pymysql.version_info = (1, 4, 13, "final", 0) pymysql.install_as_MySQLdb() # 使用pymysql代替mysqldb连接数据库 # 主线程中的全局redis链接池 # global_redis_pool的生命周期是Django主线程运行的生命周期 from utils.redis_pool import RedisPool global_redis_pool = RedisPool() # 主线程中的全局线程池 # global_thread_pool的生命周期是Django主线程运行的生命周期 from utils.thread_pool import ThreadPool global_thread_pool = ThreadPool() # This will make sure the app is always imported when # Django starts so that shared_task will use this app. from .celery import app as celery_app __all__ = ['celery_app']