​Python下的 PyQt5库 多线程执行

2023-08-10 10:40:01 浏览数 (1)

描述:公司服务器有设置一个bat文件,用来清理磁盘空间,定时每天早上8点30分执行,但是不够灵活,需要一个可以实时检测C盘,并且在C盘低于阈值时就执行bat文件清理磁盘

为什么需要执行多线程?

因为我们需要长时间执行某一个动作,如果不使用多线程,界面会卡死,导致程序异常退出,

1.导库

代码语言:Python复制
import os
import shutil
import time
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtCore import QThread

os:执行bat文件

shutil:文件操作

time:获取时间

from PyQt5 import QtCore, QtGui, QtWidgets:界面开发

import sys:显示窗体

from PyQt5.QtCore import QThread:执行多线程

2.主要代码

创建一个线程类,继承QThread在run方法里面编写我们需要的程序

这里我是首先对C盘的容量进行获取,所有空间,已用空间,剩余空间

写一个无限循环,并在剩余空间小于2G时执行bat文件

代码语言:python代码运行次数:0复制
class CrawlThread(QThread):
    def __init__(self):
        super(CrawlThread, self).__init__()
    def run(self):
        #声明一个阈值
        clean_number = float(2)
        gb = 1024 ** 3
        while 1 == 1:
            # 判断磁盘容量是否小于2G
            total_b, used_b, free_b = shutil.disk_usage('C:')
            # 将磁盘可用容量转换为GB的形式,方便阅读
            free_number = free_b / gb
            timeing2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            # 判断磁盘容量是否小于2G
            if free_number < clean_number:
                os.popen("C:/CLEANER-ks-NEW.bat")
                f = open("timingTemp.txt", "a")
                f.write(timeing2   "-"   "磁盤清理成功n")
                f.close()
            time.sleep(60)

全部代码

运行时,需要在if name == 'main':下面创建多线程执行的对象,并启动执行多线程 CrawlThread = CrawlThread() CrawlThread.start()

代码语言:python代码运行次数:0复制
# -*- coding: utf-8 -*-
import os
import shutil
import time
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtCore import QThread


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(549, 128)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(120, 20, 281, 51))
        font = QtGui.QFont()
        font.setPointSize(22)
        self.label.setFont(font)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 549, 29))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "磁盤清理"))
        self.label.setText(_translate("MainWindow", "磁盤清理運行中"))
class CrawlThread(QThread):
    def __init__(self):
        super(CrawlThread, self).__init__()
    def run(self):
        clean_number = float(2)
        gb = 1024 ** 3
        while 1 == 1:
            # 判断磁盘容量是否小于2G
            total_b, used_b, free_b = shutil.disk_usage('C:')
            # 将磁盘可用容量转换为GB的形式,方便阅读
            free_number = free_b / gb
            timeing2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            # 判断磁盘容量是否小于2G
            if free_number < clean_number:
                os.popen("C:/CLEANER-ks-NEW.bat")
                f = open("timingTemp.txt", "a")
                f.write(timeing2   "-"   "磁盤清理成功n")
                f.close()
            time.sleep(60)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    CrawlThread = CrawlThread()
    CrawlThread.start()
    sys.exit(app.exec_())

0 人点赞