在python爬虫下载的过程中,如果磁盘占用太大可能会导致系统出现问题,因此需要在下载时检测磁盘的占用空间,保证系统的正常运行。

下面为磁盘检测的代码

def space_monitor():
    ''' 当磁盘(切片存储的目录)利用率超过90%,程序退出 :return: '''

    try:
        st = os.statvfs('/')
        total = st.f_blocks * st.f_frsize
        used = (st.f_blocks - st.f_bfree) * st.f_frsize
    except FileNotFoundError:
        print('check webroot space error.')
        sys.exit(-3)

    if used / total > 0.9:
        print('No enough space.')
        logger.debug('No enough space.')
        sched.remove_job(job_id='id_space_monitor')
        sched.shutdown(wait=False)

        # 杀掉进程
        os.killpg(os.getpgid(os.getpid()), signal.SIGKILL)

        # 退出
        exit(-3)
最后修改:2019 年 05 月 06 日 11 : 33 AM