#!/usr/bin/env python

import sys
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('what', nargs=1, choices=["PySide2", "PyQt5"])
result = parser.parse_args()
PS = result.what == ["PySide2"]

if PS:
    from PySide2.QtCore import QLibraryInfo, QThread
    from PySide2.QtWidgets import (QApplication, QWidget, QPushButton,
                                   QComboBox, QVBoxLayout)
else:
    from PyQt5.QtCore import QLibraryInfo, Qt, QThread
    from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton,
                                 QComboBox, QVBoxLayout)

#sys.setswitchinterval(sys.getswitchinterval() / 100)
import time

#QThread.currentThread().setPriority(QThread.HighPriority)

class Task(QThread):
    taskno = 0

    def run(self):
        self.__class__.taskno += 1
        taskno = self.__class__.taskno
        # some heavy tasks
        print('task started')
        k = 0
        for i in range(10000):
            for j in range(50000):
                k += 1
                # this helps PySide2 when there are only few tasks.
                if k % 100 == 0:
                    time.sleep(0)
                if k % 1000000 == 0:
                    print(taskno, end=" ")
                    sys.stdout.flush()
        print('task finished')

class Gui(QWidget):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        button = QPushButton('click me')
        button.clicked.connect(self.do_task)
        combobox = QComboBox()
        combobox.addItems(['1', '2', '3', '4', '5'])
        layout.addWidget(button)
        layout.addWidget(combobox)
        self.setLayout(layout)

    def do_task(self):
        self.thread = Task()
        self.thread.start()
        prio = self.thread.priority()
        print(int(prio), prio)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    if PS:
        print(QLibraryInfo.build())
    window = Gui()
    window.show()
    sys.exit(app.exec_())
