Python 继承接口

在父类和子类之间难免存在方法和属性定义存在冲突。一般来说我们是先定义父类,但是当子类进行继承之后就需要注意是否遗留接口。

class Worker(object):
    def start(self, debug):
        self.thread = True
        if self.thread:
            signal.signal(signal.SIGINT, self.stop_)  # Ctrl+c
            signal.signal(signal.SIGTERM, self.stop_)  # 默认的kill
        if debug:
            # self.debug = "DEBUG"
            self.debug = "INFO"
        else:
            self.debug = "INFO"
        self.run()

    def stop_(self, signal_, stack_frame):
        logging.info("got signal: %s" % signal_)
        logging.info(self.__class__.__name__ + ' Exiting...')
        os.killpg(os.getpgid(os.getpid()), signal.SIGKILL)
        sys.exit(0)

    def run(self):
        raise NotImplementedError


class WebsiteConditionAgent(Worker):
    def __init__(self, check_interval, cocurrent):
        super(WebsiteConditionAgent, self).__init__()
        self.check_interval = check_interval
        self.cocurrent = cocurrent  # 这个只是默认值,当计算线程小于该值时,默认使用该值
        self.cocurrent_coefficient = {2: 20, 5: 50, 10: 100, 30: 300}

    def proc_for_agent(self):
        # 判断是否到检测时间
        if is_time_to_check(self.epoch, self.check_interval, self.last_time):
            self.last_time = self.epoch
            timestemp_ = timestemp(self.epoch)
            # get tasks  根据scan_frequency和probe_switch字段
            tasks_ = tasks(self.check_interval)

            # 算出线程数
            thread_nums = _compute_cocurrent(tasks_.__len__(), self.cocurrent_coefficient[self.check_interval],
                                             self.cocurrent)

            # check avaliablity of websites
            if tasks_:
                # 记录开始检测时间
                begin_check_epoch = time.time()
                # 使用线程检测/进程,默认线程了

                if self.thread:
                    rel = check_urls_concomitantly_thread(tasks_, timestemp_, self.check_interval, thread_nums)
                else:
                    rel = check_urls_concomitantly(tasks_, timestemp_, self.check_interval, thread_nums)
                rel = rel if rel else 0
                debug_info = u"thread_nums:%s;urls num: %s; begin time: %s;all urls time used: %s seconds;no checks %s urls\n" % (
                    thread_nums, tasks_.__len__(), formatted_time(begin_check_epoch), time.time() - begin_check_epoch,
                    rel)

            else:
                debug_info = u"urls num: 0\n"
            logging.info(debug_info)
            record_checktime_tofile(WEBSITE_CONDITION, self.check_interval, debug_info)

    def run(self):
        logging.info('logging level: %s' % self.debug)
        rsyn_linux_time()
        self.last_time = 0
        while 1:
            time.sleep(1)
            try:
                self.epoch = int(time.time())
                self.proc_for_agent()
            except Exception as e:
                logging.exception(e)
                time.sleep(5)

由于WebsiteConditionAgent继承了Worker,并在子类中执行run()函数,这是我们可以在super父类时仍然进行调用self.run()方法,依然没有问题。不过你依然需要注意父类遗留下来的方法若需等待使用则进行捕抓。

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Title - Artist
0:00