Python multiprocessing How to Call a Function

Viewed 30
import os
from multiprocessing import Pool
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QColorDialog, QFontDialog, QTextEdit, QFileDialog,QComboBox,QPlainTextEdit
import sys
class Ui_MainWindow(QWidget):
    def setupUi(self, MainWindow):
        self.cwd = os.getcwd()
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1061, 816)
        MainWindow.setMouseTracking(True)
        MainWindow.setTabletTracking(False)
        MainWindow.setStyleSheet("background-color:rgb(60, 60, 60)")
        MainWindow.setWindowTitle("BatchTool---------------------V1.0")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.outputtextBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.outputtextBrowser.setGeometry(QtCore.QRect(10, 130, 141, 31))
        brush = QtGui.QBrush(QtGui.QColor(60, 60, 60))
        brush.setStyle(QtCore.Qt.SolidPattern)
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette = QtGui.QPalette()
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        self.outputtextBrowser.setPalette(palette)
        self.outputtextBrowser.setText("OutputFilePath:")
        self.outputtextBrowser.setStyleSheet("border-width:0;border-style:outset;font: 12pt;color:#b2ffac ;")
        self.outputtextBrowser.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByKeyboard|QtCore.Qt.LinksAccessibleByMouse)
        self.outputtextBrowser.setObjectName("outputtextBrowser")
        self.output_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.output_lineEdit.setGeometry(QtCore.QRect(10, 160, 571, 31))
        self.output_lineEdit.setPalette(palette)
        self.output_lineEdit.setTabletTracking(False)
        self.output_lineEdit.setToolTip("")
        self.output_lineEdit.setStyleSheet("background:rgb(35, 35, 35);border-radius: 5px;border: 2px groove gray;border-width:0;border-style:outset;font:10pt;color:#b2ffac ")
        self.output_lineEdit.setText("")
        self.output_lineEdit.setPlaceholderText("")
        self.output_lineEdit.setObjectName("output_lineEdit")

        self.output_fileButton = QtWidgets.QPushButton(self.centralwidget)
        self.output_fileButton.setText("SelectOutputPath")
        self.output_fileButton.clicked.connect(self.getfilepath)
        self.output_fileButton.setEnabled(True)
        self.output_fileButton.setGeometry(QtCore.QRect(590, 160, 150, 31))
        self.output_fileButton.setPalette(palette)
        self.output_fileButton.setObjectName("pushButton_2")
        self.output_fileButton.setStyleSheet("QPushButton{\n"
            "border-top-right-radius: 20px; /*右上角圆角*/"
            "border-bottom-left-radius: 20px; /*左下角圆角*/"                          
            "background-color: #b2ffac;\n"
            "border: 2px solid rgb(0, 255, 0);\n"
            "color: black;\n"
            "font: 12pt ;\n"
            "}"
                "QPushButton:hover{\n"
                "border: 2px double #acfff9;\n"   
                 "color: #42DD95;\n"                      
                "}\n"
                "QPushButton:pressed{\n"
                "background-color: #1A573A;\n"
                "}\n"
                                       )
        self.btn_execute = QPushButton("execute",self.centralwidget,)
        self.btn_execute.setGeometry(QtCore.QRect(520,730,96,48))
        self.btn_execute.setStyleSheet("QPushButton{\n"
            "border-radius: 20px;\n"
            "background-color: rgb(255, 170, 127);\n"
            "border: 2px solid rgb(255, 85, 0);\n"
            "color: black;\n"
            "font: 18pt ;\n"
            "}"
                "QPushButton:hover{\n"
                "border: 2px double rgb(0, 255, 0);\n"   
                 "color: #acffd0;\n"                      
                "}\n"
                "QPushButton:pressed{\n"
                "background-color: rgb(255, 170, 255);\n"
                "}\n"
                                       )
        self.btn_execute.clicked.connect(self.myTimerState)

        MainWindow.setCentralWidget(self.centralwidget)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def getfilepath(self):
        selectfilepath = QFileDialog.getExistingDirectory(None,"选择文件夹","./")
        sender = self.sender()
        clickevent = sender.text()
        # if clickevent == "选择原始文件路径" :
        #     self.import_lineEdit.setText(selectfilepath)
        if clickevent == "SelectOutputPath" :
            self.output_lineEdit.setText(selectfilepath)

    def myTimerState(self):
        # Ui_MainWindow.run(self)
        with Pool(4) as pool:
            pool.map(Ui_MainWindow.run(self), range(1))

    def run(self):
        print(self.output_lineEdit.text())

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


How does multiprocessing call a function with arguments? if function "myTimerState" just use "Ui_MainWindow.run(self)",the program is running properly
if function "myTimerState" use :

with Pool(4) as pool:
    pool.map(Ui_MainWindow.run(self)range(1))

There will be an error message:

TypeError: 'NoneType' object is not callable

There's a lot of useless code,the main functions is "myTimerState" and "run",I want is that I press the "Execute" button and then execute the function "myTimerState" and the multiprocessing call function "run".

0 Answers
Related