thread.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ########################################################################
  2. # Searx-Qt - Lightweight desktop application for Searx.
  3. # Copyright (C) 2020-2022 CYBERDEViL
  4. #
  5. # This file is part of Searx-Qt.
  6. #
  7. # Searx-Qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-Qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. from PyQt5.QtCore import pyqtSignal, QThread, QObject
  22. class ThreadManagerProto(QObject):
  23. threadStarted = pyqtSignal()
  24. threadFinished = pyqtSignal()
  25. def __init__(self, parent=None):
  26. QObject.__init__(self, parent=parent)
  27. self._thread = None
  28. self._threadQueue = []
  29. def cancelAll(self):
  30. self._threadQueue.clear()
  31. self._thread.wait()
  32. def currentJobStr(self):
  33. return ""
  34. def queueCount(self):
  35. return len(self._threadQueue)
  36. def hasActiveJobs(self):
  37. return bool(
  38. self._thread or self._threadQueue
  39. )
  40. class Thread(QThread):
  41. finished = pyqtSignal(object)
  42. def __init__(self, func, args=None, kwargs=None, extra=None, parent=None):
  43. QThread.__init__(self, parent=parent)
  44. self._func = func
  45. self._args = args
  46. self._kwargs = kwargs
  47. self.extra = extra
  48. self._result = None
  49. def result(self): return self._result
  50. def run(self):
  51. if self._args and self._kwargs:
  52. self._result = self._func(*self._args, **self._kwargs)
  53. elif self._args:
  54. self._result = self._func(*self._args)
  55. elif self._kwargs:
  56. self._result = self._func(**self._kwargs)
  57. else:
  58. self._result = self._func()
  59. self.finished.emit(self)