首页 新闻 搜索 专区 学院

多线程Python爬虫程序无法调用pyqt5程序,求帮助

0
[待解决问题]

pyqt5程序

# -*- coding: UTF-8 -*-
import sys
from PyQt5.QtCore import QCoreApplication, QThread, QMutex
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QWidget, QLabel, QLineEdit, QApplication, QPushButton, QGridLayout
qmut_1 = QMutex()
class Thread_1(QThread):
    def __init__(self,image_path, portfolioname, company, username,driver,str,nameLineEdit):
        super(Thread_1, self).__init__()
        self.image_path = image_path
        self.portfolioname = portfolioname
        self.company = company
        self.username = username
        self.driver = driver
        self.str = str
        self.nameLineEdit = nameLineEdit
    def run(self):
        # qmut_1.lock()
        name = self.nameLineEdit.text()  # 获取文本框内容
        name = name.replace(" ", "")
        print('验证码: %s  ' % name)
        self.str += "'"
        self.str += name
        self.str += "')"
        eval(self.str)
        # self.QCoreApplication.quit
        self.close()
        # qmut_1.unlock()
 
class File(QWidget):
    def __init__(self, image_path, portfolioname, company, username,driver,str):
        super(File, self).__init__()
        self.image_path = image_path
        self.portfolioname = portfolioname
        self.company = company
        self.username = username
        self.driver = driver
        self.str = str
        self.initUi()
    def initUi(self):
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QLineEdit')
        layout = QGridLayout()
        lab_info = QLabel("账号")
        info = self.portfolioname + "-" + self.company + "-" + self.username
        lab_mes = QLabel(info)
        lab1 = QLabel("图片")
        lab_img = QLabel()
        lab_img.setPixmap(QPixmap(self.image_path))
        nameLabel = QLabel("验证码")
        self.nameLineEdit = QLineEdit(" ")
        layout.addWidget(lab_info, 1, 0)
        layout.addWidget(lab_mes, 1, 1)
        layout.addWidget(lab1, 2, 0)
        layout.addWidget(lab_img, 2, 1)
        layout.addWidget(nameLabel, 3, 0)
        layout.addWidget(self.nameLineEdit, 3, 1)
        layout.setColumnStretch(1, 10)
        save_Btn = QPushButton('确认')
        cancle_Btn = QPushButton('取消')
        cancle_Btn.clicked.connect(QCoreApplication.quit)
        save_Btn.clicked.connect(self.addNum)
        layout.addWidget(save_Btn)
        layout.addWidget(cancle_Btn)
        self.setLayout(layout)
        self.show()
    def addNum(self):
        name = self.nameLineEdit.text()  # 获取文本框内容
        name = name.replace(" ","")
        print('验证码: %s  ' % name)
        # self.str += "'"
        # self.str += name
        # self.str += "')"
        eval(self.str)
        # self.QCoreApplication.quit
        self.close()
        # self.thread_1 = Thread_1(self.image_path, self.portfolioname, self.company, self.username,self.driver,self.str,self.nameLineEdit)
        # self.thread_1.start()
 
def run_1(image_path, portfolioname, company, username,driver,str):
    app = QApplication(sys.argv)
    ex = File(image_path, portfolioname, company, username,driver,str)
    app.exit(app.exec_())
 
if __name__ == '__main__':
    image_path = r"q1.png"
    run_1(image_path,"12356","华夏","456789","driver","print('ls')")
 

多线程调用程序

from multiprocessing.dummy import Pool
from qtproblem import qtextend
from qtall.qt_test import MainWindow,Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
def m_thread_pool():
   image_path_1 = "q1.png"
   image_path_2 = "q2.png"
   list_re = [[image_path_1,"12356","华夏","456789","driver","print('ls')"],[image_path_2,"12356","华夏","456789","driver","print('ls')"]]
   pool = Pool(2)
   pool.map(decomposition,list_re)
def decomposition(list_re):
   image_path = list_re[0]
   portfolioname = list_re[1]
   company = list_re[2]
   username = list_re[3]
   driver = list_re[4]
   str = list_re[5]
   qtextend.run_1(image_path, portfolioname, company, username, driver, str)
   print("程序正在继续执行")
# def m_thread_pool_2():
#     mainw = MainWindow()
#     pool = Pool(8)
#     pool.map(Printl)
#
# def Printl():
#     print("1562")
if __name__ == '__main__':
   m_thread_pool()

如果将线程池内的线程数改为1,就能正常执行

Flying_boy_1的主页 Flying_boy_1 | 菜鸟二级 | 园豆:202
提问于:2021-11-17 09:51
< >
分享
所有回答(2)
0

pool.map(decomposition,list_re) 换成pool.map_async(decomposition,list_re), map是阻塞,map_async是非阻塞

小小咸鱼YwY | 园豆:2918 (老鸟四级) | 2021-11-18 09:39

改为map_async后就直接启动不了qt程序了呢

支持(0) 反对(0) Flying_boy_1 | 园豆:202 (菜鸟二级) | 2021-11-18 17:52

@Flying_boy_1: 你线程池换个位置加

支持(0) 反对(0) 小小咸鱼YwY | 园豆:2918 (老鸟四级) | 2021-11-18 18:39
0

首先PyQt5 的多线程,和python的多线程不能混用,https://wshuo.blog.csdn.net/article/details/88716205

Hello_wshuo | 园豆:140 (初学一级) | 2021-12-04 15:43

你好,我这边没有使用到pyqt5的多线程,只是使用了python的多线程去调用一个pyqt5程序,上面的pyqt5程序是我之前测试时写的,但是没有调用,不好意思。但是他这个python多线程在调用这个pyqt5程序的时候总是崩溃,对于这个有什么好的解决方法吗。

支持(0) 反对(0) Flying_boy_1 | 园豆:202 (菜鸟二级) | 2021-12-06 13:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册