Menu

[8f66b6]: / tools / AsynchronousTask.py  Maximize  Restore  History

Download this file

83 lines (70 with data), 3.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
"""
Copyright (C) 2012 Oliver Tengler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import threading
from typing import Callable, Any
from PyQt5.QtCore import QThread, pyqtSlot, QObject
from dialogs.ProgressBar import ProgressBar
CancelFunction = Callable[[],None]
class AsynchronousTask (QThread):
def __init__(self, function: Callable, *args: Any, bEnableCancel: bool=False, cancelAction: CancelFunction=None) -> None:
super().__init__(None) # Called with None to get rid of the thread once the python object is destroyed
self.function = function
self.args = args
self.bEnableCancel = bEnableCancel
self.cancelAction = cancelAction
self.result: Any = None
self.exception: Exception
self.hasException = False
self.cancelEvent = None
if self.bEnableCancel:
self.cancelEvent = threading.Event()
def run(self) -> None:
try:
if self.cancelEvent:
self.result = self.function(*self.args, cancelEvent=self.cancelEvent)
else:
self.result = self.function(*self.args)
except Exception as e:
self.exception = e
self.hasException = True
@pyqtSlot()
def cancel(self) -> None:
if self.cancelEvent:
self.cancelEvent.set()
if self.cancelAction:
self.cancelAction()
def execute(parent: QObject, func:Callable, *args: Any, bEnableCancel: bool=False, cancelAction: CancelFunction=None) -> Any:
"""
Executes the action performed by the callable 'func' called with *args in a seperate thread.
During the action a progress bar is shown. If 'bEnableCancel' is true the callable is
passed the named parameter 'cancelEvent'. It contains an event object whose 'is_set' function
can be used to test if it is signalled.
"""
try:
progress = ProgressBar(parent, bEnableCancel)
searchTask = AsynchronousTask(func, *args, bEnableCancel=bEnableCancel, cancelAction=cancelAction)
searchTask.finished.connect(progress.close)
progress.onCancelClicked.connect(searchTask.cancel)
progress.show()
searchTask.start()
progress.exec()
searchTask.wait()
# If the thread raised an exception re-raise it in the main thread
if searchTask.hasException:
raise searchTask.exception
return searchTask.result
finally:
if progress:
progress.close()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.