noxdafox / pebble

Multi threading and processing eye-candy.
GNU Lesser General Public License v3.0
525 stars 49 forks source link

Pebble when used with PyQT5 is generating multiple UI instances #107

Closed ItzmeSwapy closed 1 year ago

ItzmeSwapy commented 1 year ago

Instead of multithreading the function the whole UI is getting multithreaded with no outcome.

ItzmeSwapy commented 1 year ago

`import openpyxl from functionalities.proxy_livetester import check_proxy from PyQt5 import QtWidgets import sys from pebble import ProcessPool from concurrent.futures import TimeoutError

class mywindow(QtWidgets.QMainWindow): group_id_col_number = -1 def init(self): super(mywindow, self).init() uic.loadUi(r'ui_Files/IAM_MainUI.ui', self) self.data_reload_button_2.clicked.connect(self.testfunc)

def proxy_checkers(self,proxy_dict):
    # checks all the proxies working status and returns good proxies
    proxies_thread = []
    working_proxies = []
    proxy_timeout_limit = 100
    with ProcessPool() as pool:
        for x in proxy_dict:
            proxies_thread.append(pool.submit(check_proxy, proxy_timeout_limit, x))

        proxy_failcount = 0
        for i, single_proxy_thread in enumerate(proxies_thread):
            try:
                proxy_status = single_proxy_thread.result()
                # print(type(proxy_status), proxy_status)
            except TimeoutError:
                print("Proxy Failed %s" % single_proxy_thread)
                proxy_failcount += 1

            if (type(proxy_status) == str): proxy_status = proxy_status.split('|')
            if proxy_status[1] != 'None':
                working_proxies.append(proxy_status[0])
            else:
                proxy_failcount += 1
            # print(working_proxies)
    print("Total Working Proxies:", len(working_proxies), "Total Failed Proxies:", proxy_failcount)
    return working_proxies

def proxies(self,proxy_file):
    wb_obj = openpyxl.load_workbook(proxy_file)
    sheet_obj = wb_obj.active
    max_col = sheet_obj.max_column
    max_row = sheet_obj.max_row
    proxy_dic = {}
    for i in range(1, max_row + 1):
        proxy_id = sheet_obj.cell(row=i, column=1)
        proxy_reset = sheet_obj.cell(row=i, column=2)
        proxy_dic[proxy_id.value] = proxy_reset.value
    return proxy_dic

def testfunc(self):
    proxy_file = r'C:\Bots\Instagram\src\proxies\selected_proxy.xlsx'
    proxy_dictionary=self.proxies(proxy_file)
    self.proxy_checkers(proxy_dictionary)

app = QtWidgets.QApplication([]) application = mywindow() application.show() sys.exit(app.exec())`

Makes this happen !image(https://user-images.githubusercontent.com/42511941/193474994-cdacb9a5-23c4-4f4d-a140-278e593633b2.jpeg)

ItzmeSwapy commented 1 year ago

This is my program structure:

Main.py - has a UI class through it's init function it calls the action function which has the pebble code.(pebble code targets function from a different module.)

Module --> function that needs to be multithreaded.

noxdafox commented 1 year ago

Hello,

please fix the formatting of the previous messages, the code is right now unreadable. I am not familiar with QT library.

Have you tried the same code with standard Python concurrent.futures.ProcessPoolExecutor? Do you experience similar issues?

ItzmeSwapy commented 1 year ago

Hey @noxdafox I have tried the same code with concurrent.futures.ProcessPoolExecutor it works fine no issues but the problem is when I use pebble in the same format as concurrent.futures.ProcessPoolExecutor . The problem is that it makes multiple UI instances that actually goes into a infinite instance loop.

Here is a readable code: https://gist.github.com/ItzmeSwapy/c4e05b3cc5b13a050cacf2036ffc31ae

dcnieho commented 1 year ago

you need to put

app = QtWidgets.QApplication([])
application = mywindow()
application.show()
sys.exit(app.exec())`

under an if __name__=="main": else it gets executed by each process again

ItzmeSwapy commented 1 year ago

thanks man @dcnieho you saved my life. It works finally after 3days of pain.

This was a foolish mistake on my part