Dec-28-2018, 03:34 PM
I'm trying to make a program using PRAW and PyQt that gathers Reddit posts from subscribed subreddits and displays them in a GUI window. The code I currently have seems to work, but each time the program tries to gather posts, the program slows down so much, it is unusable. The program manages retrieving posts by running a function every 5 seconds using a QTimer, as shown in the following code:
class App: #... def check(self): self.model.checkPosts() def __init__(self): self.model = Model(self) self.app = QApplication([]) self.app.setStyle("Fusion") self.timer = QTimer() self.timer.setInterval(5000) self.timer.timeout.connect(self.check) #... class Model: #... def checkPosts(self): subs = self.reddit.user.subreddits() try: for sub in subs: posts = sub.hot(limit=1) for post in posts: if not(post.title in self.postList): NewPostController(self.app, self).addPost(post) except NotFound as error: print("Subreddit not found") except Redirect as error: print("Subreddit not found") except InvalidToken as error: self.auth(self.user,self.password) #... class NewPostController: #... def addPost(self, post): self.model.postList.append(post.title) link = LinkWidget(post=post) self.app.layout.addWidget(link) class LinkWidget(QLabel): def __init__(self, parent=None, post=None): QLabel.__init__(self,parent=parent); self.setFrameShape(QFrame.Shape.StyledPanel) self.setFrameShadow(QFrame.Shadow.Raised) self.setMaximumWidth(self.window().frameGeometry().width()) self.setAutoFillBackground(True) palette = QPalette() palette.setColor(QPalette.Background, Qt.white) self.setPalette(palette) self.setText('<a href="' + post.url + '" style="text-decoration:none;">'+post.title+"</a>") self.setOpenExternalLinks(True) self.setWordWrap(True) font = QFont("Arial", 17, QFont.Bold) self.setFont(font)I've tried using the threading module to execute the Model.checkThreads() in a separate thread to prevent the slowdown, but it didn't do anything. Is there any way I can prevent the slowdown from happening? Is my code inefficient?