I have GUI and program logic written in Python. I am requesting information from web by calling urllib.requests
(and so on) very often and this cause a problem when GUI is unresponsive but this calls are wrapped with QThread
. I think that happens because of GIL
. But how when I can use QThread
in PyQt application, what use of it in PyQt
if I can't make code to work asynchronously?
--The code--
qtthreaddecorator.py:
from PyQt4 import QtCore
class Worker(QtCore.QThread):
def __init__(self, thread_name, finished_slot, function, *args, **kwargs):
QtCore.QThread.__init__(self)
self._thread_name = thread_name
self._function = function
self._args = args
self._kwargs = kwargs
self._finished_slot = finished_slot
def run(self):
self._function(*self._args, **self._kwargs)
self._finished_slot()
return
def qt_thread_decorator(slot):
def decorator(function):
def wrapper(*args, **kwargs):
worker = Worker(function.__name__, slot, function, *args, **kwargs)
worker.start()
return
return wrapper
return decorator
And the place where I am using it:
import qtthreaddecorator
class MainWindow(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.init()
def init(self):
@qtthreaddecorator.qt_thread_decorator(self._fill_servers)
def _get_servers():
self._get_my_servers()
@qtthreaddecorator.qt_thread_decorator(self._fill_user_info)
def _get_user_info():
self._get_user_info()
_get_servers()
_get_user_info()
In my case, _get_servers()
and _get_user_info()
calls in order but I want to execute them concurrently.