Python Forum

Full Version: PRAW and PyQt: Immense slowdown when retrieving Reddit posts and adding them to GUI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
What exactly is slow? If you're scraping a website, they might throttle you if they detect it.

We can't figure out what part is slow just looking at the code, so you need to do the experiments to figure which exact line(s) are problematic and then we might be of more help.
(Dec-28-2018, 05:49 PM)micseydel Wrote: [ -> ]What exactly is slow? If you're scraping a website, they might throttle you if they detect it.

We can't figure out what part is slow just looking at the code, so you need to do the experiments to figure which exact line(s) are problematic and then we might be of more help.

Thanks for the suggestion. It turns out the checkMethods function kept fetching the user's subscribed subreddits every time it was run, and it was costing a lot of time.