0

I have tried some suggestions on the net but couldn't succeed yet. Not sure if it is possible but will explain what I am trying to achieve.

I have a MainWindow which has the following method. And a button initializes three instances of browser widgets beforehand.

def handleSearches(self):
        # do the search
        browseThreads = []
        for idx, browser in enumerate(self.browsers):
            browseThreads.append(BrowseThread(browser, self.data["options"][idx]))

        for browseThread in browseThreads:
            browseThread.start()

Where the browser as defined QWidget like following;

class Browser(QtWidgets.QWidget):
    def __init__(self, title):
        super().__init__()

        self.gridLayout = QtWidgets.QGridLayout(self)
        self.gridLayout.setObjectName("gridLayout")

        self.webView = QWebEngineView()
        self.webView.setUrl(QtCore.QUrl("https://www.google.com.tr"))

        # some codes

    def search(self, question, candidate, mode):
            self.candidate = candidate
            url = "https://www.google.com.tr/search?q={}".format(urllib.parse.quote_plus(candidate))

        self.webView.setUrl(QtCore.QUrl(url))


    def find_text(self, text):
        soup = BS(self.src,'lxml')

        # some source code alteration here

        self.webView.page().setHtml(str(soup), self.webView.page().requestedUrl())

    def on_page_load(self):
        self.webView.page().toHtml(self.on_source_fetched)

    def on_source_fetched(self, data):
            self.src = data
            self.find_text(self.candidate)

My Thread class is defined like following,

class BrowseThread (QThread):
    def __init__(self, browser, searchString):
        QThread.__init__(self)
        self.searchString = searchString
        self.browser = browser

    def run(self):
        self.search_text(self.searchString)

    def search_text(self, text):
        self.browser.search(text, '', 1)

What I am trying to achieve is to search different strings in parallel in 3 instances without blocking anything. Thread didn't work I guess because the browser widget GUI needs an update. How to do this?

Thanks.

Isma
  • 14,604
  • 5
  • 37
  • 51
freezer
  • 531
  • 1
  • 11
  • 28
  • the GUI can not be executed in another thread, this is forbidden by Qt. Also multithreading is not parallelism, and instead of getting benefits you are getting problems – eyllanesc Jan 03 '19 at 03:40
  • Do you have any heavy tasks that can block the GUI? – eyllanesc Jan 03 '19 at 03:49
  • When I try to send search strings to "browsers" in for loop GUI is block for a few seconds and does some weird behavior. I need to finish this search in 3 window in 5 - 6 seconds. So the GUI should not freeze and page load should not wait for other "browsers". When the page loads I detect some html elements and change their html codes and reload the html source code into webview to see the detections. – freezer Jan 03 '19 at 07:07
  • I think the task that brings you problems is *# some codes*, you could show an example where that problem will generate you, and I could give you a more precise example of the logic that prevents the window from freezing. – eyllanesc Jan 03 '19 at 07:10
  • It is not that part cause the problem.That part only includes layout initialization. Problem rises when search function is executed for each browser. I tried Signal and Slot aproach. Seems working better but not good enough. Memory use is increases after each search and qtwebengine tasks stay openned in the task manager after I stop the compiler.. Causes pc to slow down. – freezer Jan 03 '19 at 15:37
  • sorry, you're right, I wanted to point out another part of your code: *# some source code alteration here*, the task of loading a page takes a while since there are web requests that are not instantaneous and that Qt handles them, – eyllanesc Jan 03 '19 at 15:42
  • [cont.] but the tasks of scrapy that uses tools that are not implemented by Qt if they take a long time freeze the GUI because obviously they work synchronously, and not asynchronously as Qt waits for these cases so I recommend you do that part that runs on another thread having the careful not to modify the GUI directly but using threads and signals, but I see that you do not know that concept very well and you can fail, so that is my motive to ask you for an [MCVE]. – eyllanesc Jan 03 '19 at 15:43
  • I am on mobile. As soon as ı got to pc will check it. Thanks for your interest. – freezer Jan 03 '19 at 15:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186134/discussion-between-freezer-and-eyllanesc). – freezer Jan 03 '19 at 20:15

0 Answers0