-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
HydrusPubSub.py
215 lines (99 loc) · 6.54 KB
/
HydrusPubSub.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from hydrus.core import HydrusData
from hydrus.core import HydrusExceptions
import threading
import weakref
from hydrus.core import HydrusGlobals as HG
class HydrusPubSub( object ):
def __init__( self, controller, valid_callable ):
self._controller = controller
self._valid_callable = valid_callable
self._doing_work = False
self._pubsubs = []
self._pub_event = threading.Event()
self._lock = threading.Lock()
self._topics_to_objects = {}
self._topics_to_method_names = {}
def _GetCallableTuples( self, topic ):
# this now does the obj as well so we have a strong direct ref to it throughout procesing
callable_tuples = []
if topic in self._topics_to_objects:
try:
objects = self._topics_to_objects[ topic ]
for obj in objects:
if obj is None or not self._valid_callable( obj ):
continue
method_names = self._topics_to_method_names[ topic ]
for method_name in method_names:
if hasattr( obj, method_name ):
callable = getattr( obj, method_name )
callable_tuples.append( ( obj, callable ) )
except:
pass
return callable_tuples
def DoingWork( self ):
return self._doing_work
def Process( self ):
# only do one list of callables at a time
# we don't want to map a topic to its callables until the previous topic's callables have been fully executed
# e.g. when we start a message with a pubsub, it'll take a while (in independant thread-time) for Qt to create
# the dialog and hence map the new callable to the topic. this was leading to messages not being updated
# because the (short) processing thread finished and entirely pubsubbed before Qt had a chance to boot the
# message.
self._doing_work = True
try:
callable_tuples = []
with self._lock:
if len( self._pubsubs ) == 0:
return
pubsubs = self._pubsubs
self._pubsubs = []
for ( topic, args, kwargs ) in pubsubs:
try:
# do all this _outside_ the lock, lol
callable_tuples = self._GetCallableTuples( topic )
# don't want to report the showtext we just send here!
not_a_report = topic != 'message'
if HG.pubsub_report_mode and not_a_report:
HydrusData.ShowText( ( topic, args, kwargs, callable_tuples ) )
if HG.pubsub_profile_mode and not_a_report:
summary = 'Profiling ' + HydrusData.ToHumanInt( len( callable_tuples ) ) + ' x ' + topic
HydrusData.ShowText( summary )
per_summary = 'Profiling ' + topic
for ( obj, callable ) in callable_tuples:
try:
HydrusData.Profile( per_summary, 'callable( *args, **kwargs )', globals(), locals() )
except HydrusExceptions.ShutdownException:
return False
else:
for ( obj, callable ) in callable_tuples:
try:
callable( *args, **kwargs )
except HydrusExceptions.ShutdownException:
return False
except Exception as e:
HydrusData.ShowException( e )
finally:
self._doing_work = False
def pub( self, topic, *args, **kwargs ):
with self._lock:
self._pubsubs.append( ( topic, args, kwargs ) )
self._pub_event.set()
def pubimmediate( self, topic, *args, **kwargs ):
with self._lock:
callable_tuples = self._GetCallableTuples( topic )
for ( obj, callable ) in callable_tuples:
callable( *args, **kwargs )
def sub( self, object, method_name, topic ):
with self._lock:
if topic not in self._topics_to_objects: self._topics_to_objects[ topic ] = weakref.WeakSet()
if topic not in self._topics_to_method_names: self._topics_to_method_names[ topic ] = set()
self._topics_to_objects[ topic ].add( object )
self._topics_to_method_names[ topic ].add( method_name )
def WaitOnPub( self ):
self._pub_event.wait( 3 )
self._pub_event.clear()
def Wake( self ):
self._pub_event.set()
def WorkToDo( self ):
with self._lock:
return len( self._pubsubs ) > 0