Я хочу сделать программу отображения сообщений с форума "X". Я сделал модуль с webscrapping, чтобы получить все данные, которые я хочу. Это работает правильно. Используя PyQt5 я сделал GUI. Это выглядит так -> GUI. Каждое поле для данных, таких как author_1, title_1 и c, представляет собой метку pyqt5.
Я хочу обновлять каждую строку (заголовок, автора, час, просмотры, ответы) каждые «X» секунд. Я впервые использую темы, поэтому есть вероятность, что я не понимаю, как мне это использовать. Я создал 20 потоков, каждый поток отвечает за обновление данных в своем ряду.
Сейчас обновление занимает 10-15 секунд. Я хочу сделать это быстрее. Как мне это сделать? Если вы можете дать мне совет, что я должен делать иначе, чем я. Я хочу, чтобы эта программа работала настолько хорошо, насколько это возможно. Код ниже.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from datetime import datetime
import list_of_post as list # MY WEBSCRAPPING MODULE
import time
class Worker(QRunnable):
'''
Worker thread
Inherits from QRunnable to handler worker thread setup, signals and wrap-up.
:param callback: The function callback to run on this worker thread. Supplied args and
kwargs will be passed through to the runner.
:type callback: function
:param args: Arguments to pass to the callback function
:param kwargs: Keywords to pass to the callback function
'''
def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()
# Store constructor arguments (re-used for processing)
self.fn = fn
self.args = args
self.kwargs = kwargs
def run(self):
'''
Initialise the runner function with passed args, kwargs.
'''
self.fn(*self.args, **self.kwargs)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#SET UP OF MAIN WINDOW. I CUT NOT IMPORTANT DATA FOR THIS QUESTION
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.threadpool = QThreadPool()
self.threadpool.setMaxThreadCount(25)
print("Multithreading with maximum %d threads" % self.threadpool.maxThreadCount())
def retranslateUi(self, MainWindow):
### HERE IS SET UP. I CUT IT BECAUSE I THINK ITS NOT IMPORTANT
def update_1(self):
print("UPDATE STARTED")
self.title_1.setText(main_list.return_title_data(0))
self.author_1.setText(main_list.return_author_data(0))
self.hour_1.setText(main_list.return_time_data(0))
self.views_1.setText(main_list.return_views_data(0))
self.answers_1.setText(main_list.return_answears_data(0))
print("UPDATE ENDED")
def update_2(self):
self.title_2.setText(main_list.return_title_data(1))
self.author_2.setText(main_list.return_author_data(1))
self.hour_2.setText(main_list.return_time_data(1))
self.views_2.setText(main_list.return_views_data(1))
self.answers_2.setText(main_list.return_answears_data(1))
def update_3(self):
self.title_3.setText(main_list.return_title_data(2))
self.author_3.setText(main_list.return_author_data(2))
self.hour_3.setText(main_list.return_time_data(2))
self.views_3.setText(main_list.return_views_data(2))
self.answers_3.setText(main_list.return_answears_data(2))
def update_4(self):
self.title_4.setText(main_list.return_title_data(3))
self.author_4.setText(main_list.return_author_data(3))
self.hour_4.setText(main_list.return_time_data(3))
self.views_4.setText(main_list.return_views_data(3))
self.answers_4.setText(main_list.return_answears_data(3))
def update_5(self):
self.title_5.setText(main_list.return_title_data(4))
self.author_5.setText(main_list.return_author_data(4))
self.hour_5.setText(main_list.return_time_data(4))
self.views_5.setText(main_list.return_views_data(4))
self.answers_5.setText(main_list.return_answears_data(4))
def update_6(self):
self.title_6.setText(main_list.return_title_data(5))
self.author_6.setText(main_list.return_author_data(5))
self.hour_6.setText(main_list.return_time_data(5))
self.views_6.setText(main_list.return_views_data(5))
self.answers_6.setText(main_list.return_answears_data(5))
def update_7(self):
self.title_7.setText(main_list.return_title_data(6))
self.author_7.setText(main_list.return_author_data(6))
self.hour_7.setText(main_list.return_time_data(6))
self.views_7.setText(main_list.return_views_data(6))
self.answers_7.setText(main_list.return_answears_data(6))
def update_8(self):
self.title_8.setText(main_list.return_title_data(7))
self.author_8.setText(main_list.return_author_data(7))
self.hour_8.setText(main_list.return_time_data(7))
self.views_8.setText(main_list.return_views_data(7))
self.answers_8.setText(main_list.return_answears_data(7))
def update_9(self):
self.title_9.setText(main_list.return_title_data(8))
self.author_9.setText(main_list.return_author_data(8))
self.hour_9.setText(main_list.return_time_data(8))
self.views_9.setText(main_list.return_views_data(8))
self.answers_9.setText(main_list.return_answears_data(8))
def update_10(self):
self.title_10.setText(main_list.return_title_data(9))
self.author_10.setText(main_list.return_author_data(9))
self.hour_10.setText(main_list.return_time_data(9))
self.views_10.setText(main_list.return_views_data(9))
self.answers_10.setText(main_list.return_answears_data(9))
def update_11(self):
self.title_11.setText(main_list.return_title_data(10))
self.author_11.setText(main_list.return_author_data(10))
self.hour_11.setText(main_list.return_time_data(10))
self.views_11.setText(main_list.return_views_data(10))
self.answers_11.setText(main_list.return_answears_data(10))
def update_12(self):
self.title_12.setText(main_list.return_title_data(11))
self.author_12.setText(main_list.return_author_data(11))
self.hour_12.setText(main_list.return_time_data(11))
self.views_12.setText(main_list.return_views_data(11))
self.answers_12.setText(main_list.return_answears_data(11))
def update_13(self):
self.title_13.setText(main_list.return_title_data(12))
self.author_13.setText(main_list.return_author_data(12))
self.hour_13.setText(main_list.return_time_data(12))
self.views_13.setText(main_list.return_views_data(12))
self.answers_13.setText(main_list.return_answears_data(12))
def update_14(self):
self.title_14.setText(main_list.return_title_data(13))
self.author_14.setText(main_list.return_author_data(13))
self.hour_14.setText(main_list.return_time_data(13))
self.views_14.setText(main_list.return_views_data(13))
self.answers_14.setText(main_list.return_answears_data(13))
def update_15(self):
self.title_15.setText(main_list.return_title_data(14))
self.author_15.setText(main_list.return_author_data(14))
self.hour_15.setText(main_list.return_time_data(14))
self.views_15.setText(main_list.return_views_data(14))
self.answers_15.setText(main_list.return_answears_data(14))
def update_16(self):
self.title_16.setText(main_list.return_title_data(15))
self.author_16.setText(main_list.return_author_data(15))
self.hour_16.setText(main_list.return_time_data(15))
self.views_16.setText(main_list.return_views_data(15))
self.answers_16.setText(main_list.return_answears_data(15))
def update_17(self):
self.title_17.setText(main_list.return_title_data(16))
self.author_17.setText(main_list.return_author_data(16))
self.hour_17.setText(main_list.return_time_data(16))
self.views_17.setText(main_list.return_views_data(16))
self.answers_17.setText(main_list.return_answears_data(16))
def update_18(self):
self.title_18.setText(main_list.return_title_data(17))
self.author_18.setText(main_list.return_author_data(17))
self.hour_18.setText(main_list.return_time_data(17))
self.views_18.setText(main_list.return_views_data(17))
self.answers_18.setText(main_list.return_answears_data(17))
def update_19(self):
self.title_19.setText(main_list.return_title_data(18))
self.author_19.setText(main_list.return_author_data(18))
self.hour_19.setText(main_list.return_time_data(18))
self.views_19.setText(main_list.return_views_data(18))
self.answers_19.setText(main_list.return_answears_data(18))
def update_20(self):
self.title_20.setText(main_list.return_title_data(19))
self.author_20.setText(main_list.return_author_data(19))
self.hour_20.setText(main_list.return_time_data(19))
self.views_20.setText(main_list.return_views_data(19))
self.answers_20.setText(main_list.return_answears_data(19))
def time_update_change(self): # METHOD CHANING INFORMATION ABOUT LAST UPDATE (LEFT DOWN CORNER IN GUI)
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
self.last_update.setText(dt_string)
def timer(self):
while True:
time.sleep(90) # HOW CAN I CHANGE IT? I KNOW ITS NOT THE BEST IDEA TO MAKE METHOD SLEEP FOR 90 sec...
worker_1 = Worker(self.update_1)
worker_2 = Worker(self.update_2)
worker_3 = Worker(self.update_3)
worker_4 = Worker(self.update_4)
worker_5 = Worker(self.update_5)
worker_6 = Worker(self.update_6)
worker_7 = Worker(self.update_7)
worker_8 = Worker(self.update_8)
worker_9 = Worker(self.update_9)
worker_10 = Worker(self.update_10)
worker_11 = Worker(self.update_11)
worker_12 = Worker(self.update_12)
worker_13 = Worker(self.update_13)
worker_14 = Worker(self.update_14)
worker_15 = Worker(self.update_15)
worker_16 = Worker(self.update_16)
worker_17 = Worker(self.update_17)
worker_18 = Worker(self.update_18)
worker_19 = Worker(self.update_19)
worker_20 = Worker(self.update_20)
worker_date_update = Worker(self.time_update_change)
self.threadpool.start(worker_1)
self.threadpool.start(worker_2)
self.threadpool.start(worker_3)
self.threadpool.start(worker_4)
self.threadpool.start(worker_5)
self.threadpool.start(worker_6)
self.threadpool.start(worker_7)
self.threadpool.start(worker_8)
self.threadpool.start(worker_9)
self.threadpool.start(worker_10)
self.threadpool.start(worker_11)
self.threadpool.start(worker_12)
self.threadpool.start(worker_13)
self.threadpool.start(worker_14)
self.threadpool.start(worker_15)
self.threadpool.start(worker_16)
self.threadpool.start(worker_17)
self.threadpool.start(worker_18)
self.threadpool.start(worker_19)
self.threadpool.start(worker_20)
self.threadpool.start(worker_date_update)
def new_thread_update(self):
worker = Worker(self.timer)
self.threadpool.start(worker)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
main_list = list.List_of_posts() # LIST WITH WEBSCRAPPED DATA FROM FORUM
ui.new_thread_update() #update
sys.exit(app.exec_())
В коде я удалил это, что я думал, что это не важно. Я дал комментарии об этом в коде. Спасибо за ваше время!