This repository was archived by the owner on Sep 1, 2023. It is now read-only.
forked from python-mode/python-mode
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue.py
More file actions
78 lines (57 loc) · 1.71 KB
/
Copy pathqueue.py
File metadata and controls
78 lines (57 loc) · 1.71 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
from __future__ import absolute_import
import threading
from Queue import Queue, Empty
from .interface import show_message
MAX_LIFE = 60
CHECK_INTERVAL = .2
RESULTS = Queue()
TEST = 1
class Task(threading.Thread):
def __init__(self, *args, **kwargs):
threading.Thread.__init__(self, *args, **kwargs)
self.stop = threading.Event()
def run(self):
""" Run the task.
"""
try:
args, kwargs = self._Thread__args, self._Thread__kwargs
checking = self._Thread__target(*args, **kwargs)
if not self.stop.isSet():
RESULTS.put((checking, args, kwargs))
except Exception as e:
if not self.stop.isSet():
RESULTS.put(e)
def add_task(target, title=None, *args, **kwargs):
" Add all tasks. "
# Only one task at time
for thread in threading.enumerate():
if isinstance(thread, Task):
return True
task = Task(target=target, args=args, kwargs=kwargs)
task.daemon = True
task.start()
show_message('{0} started.'.format(title))
def stop_queue(message=True):
""" Stop all tasks.
"""
with RESULTS.mutex:
RESULTS.queue.clear()
for thread in threading.enumerate():
if isinstance(thread, Task):
thread.stop.set()
if message:
show_message("Task stopped.")
def check_task():
""" Checking running tasks.
"""
try:
result = RESULTS.get(False)
assert isinstance(result, tuple)
except Empty:
return False
except AssertionError:
return False
result, _, kwargs = result
callback = kwargs.pop('callback')
callback(result, **kwargs)
# lint_ignore=W0703