thread.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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()
  42. def __init__(self, func, args=None, kwargs=None, parent=None):
  43. QThread.__init__(self, parent=parent)
  44. self._func = func
  45. self._args = args
  46. self._kwargs = kwargs
  47. self._result = None
  48. def result(self): return self._result
  49. def run(self):
  50. if self._args and self._kwargs:
  51. self._result = self._func(*self._args, **self._kwargs)
  52. elif self._args:
  53. self._result = self._func(*self._args)
  54. elif self._kwargs:
  55. self._result = self._func(**self._kwargs)
  56. else:
  57. self._result = self._func()
  58. self.finished.emit()