3 import delayedresult
as dr
7 ss
=dr
.Struct(a
='a', b
='b')
13 def handler(b
, d
, a
=None, c
=None):
18 hh
=dr
.Handler(handler
, 1, a
='a')
21 def handler2(*args
, **kwargs
):
24 assert kwargs
['a'] == 'a'
25 assert kwargs
['b'] == 'b'
26 hh2
= dr
.Handler(handler2
, 1, a
='a')
28 hh3
= dr
.Handler(hh2
, b
='b', *args
)
33 triplet
= (1,'a',2.34)
34 b
= dr
.Struct(called
=False, which
=1)
36 def consumer(result
, a
, b
=None):
37 assert result
.get() == 789 + b
.which
38 assert result
.getJobID() == 456
41 handler
= dr
.Handler(consumer
, 'a', **dict(b
=b
))
42 ss
= dr
.SenderNoWx( handler
, jobID
=456 )
51 raise RuntimeError('should have raised!')
52 except AssertionError:
54 ss
= dr
.SenderNoWx( dr
.Handler(consumer
) )
55 ss
.sendException( AssertionError('test') )
59 expect
= dr
.Struct(value
=1)
61 assert result
.getJobID() is None
62 assert result
.get() == expect
.value
64 ss
= dr
.SenderNoWx( dr
.Handler(consumer
) )
66 def worker(sender
=None):
74 tt
= dr
.Producer(ss
, worker
, senderArg
='sender')
76 while expect
.value
< 7:
81 def testStartWorker():
82 print 'Doing worker thread with call-after'
84 def handleButtonClick():
85 produce
= [123, 456, 789, 012, 345, 678, 901]
86 expect
= dr
.Struct(idx
=0)
87 def worker(a
, b
=None, sender
=None):
92 sender
.sendResult(val
)
93 def consumer(result
, b
, a
=None):
97 print 'got result', result
98 if expect
.idx
< len(produce
):
99 assert result
== produce
[ expect
.idx
]#, 'Expected %s, got %s' % (
101 assert result
is None
105 dr
.startWorker(consumer
, worker
, cargs
=(1,), ckwargs
={'a':'a'}
,
106 wargs
=(2,), wkwargs
={'b':'b'}
, senderArg
='sender')
108 app
= wx
.PySimpleApp()
109 frame
= wx
.Frame(None) # need this otherwise MainLoop() returns immediately
110 # pretend user has clicked:
112 thread
.start_new_thread( wx
.CallAfter
, (handleButtonClick
,))
116 def testStartWorkerEvent():
117 print 'Doing same with events'
119 produce
= [123, 456, 789, 012, 345, 678, 901]
120 expect
= dr
.Struct(idx
=0)
121 def worker(a
, b
=None, sender
=None):
126 sender
.sendResult(val
)
129 result
= event
.result
.get()
130 print 'got result', result
131 if expect
.idx
< len(produce
):
132 assert result
== produce
[ expect
.idx
]#, 'Expected %s, got %s' % (
134 assert result
is None
137 def handleButtonClick():
138 dr
.startWorker(frame
, worker
,
139 cargs
=(eventClass
,), ckwargs
={'a':'a','resultAttr':'result'}
,
140 wargs
=(2,), wkwargs
={'b':'b'}
, senderArg
='sender')
142 app
= wx
.PySimpleApp()
143 frame
= wx
.Frame(None) # need this otherwise MainLoop() returns immediately
144 from wx
.lib
.newevent
import NewEvent
as wxNewEvent
145 eventClass
, eventBinder
= wxNewEvent()
146 frame
.Bind(eventBinder
, consumer
)
147 # pretend user has clicked:
149 thread
.start_new_thread( wx
.CallAfter
, (handleButtonClick
,))
155 abort
= dr
.AbortEvent()
157 # create a wx app and a function that will cause
158 # app to close when abort occurs
159 app
= wx
.PySimpleApp()
160 frame
= wx
.Frame(None) # need this otherwise MainLoop() returns immediately
163 # make sure any events have time to be processed before exit
164 wx
.FutureCall(2000, app
.ExitMainLoop
)
165 threading
.Thread(target
=exiter
).start()
167 # now do the delayed result computation:
171 print 'Result computation not done, not aborted'
172 return 'Result computed'
173 def consumer(dr
): # never gets called but as example
174 print 'Got dr=', dr
.get()
176 dr
.startWorker(consumer
, worker
)
178 # pretend user doing other stuff
181 # pretend user aborts now:
182 print 'Setting abort event'
187 def testPreProcChain():
190 assert dr
.getJobID() == 123
191 assert dr
.get() == 321
192 pp
=dr
.PreProcessChain( handler
)
193 pp( dr
.DelayedResult(321, jobID
=123) )
196 def handlerPP(chainTrav
, n
, a
=None):
200 assert chainTrav
.getJobID() == 321
201 res
= chainTrav
.get()
203 print 'Done handlerPP'
205 def subStart1(handler
):
206 pp
=dr
.PreProcessChain(handler
)
207 pp
.addSub(subEnd1
, 1, b
='b')
208 subStart2(pp
.clone())
209 def subEnd1(chainTrav
, aa
, b
=None):
213 assert chainTrav
.getJobID() == 321
214 res
= chainTrav
.get()
215 assert res
== 246, 'res=%s' % res
216 print 'Returning from subEnd1'
219 def subStart2(preProc
):
220 preProc
.addSub(subEnd2
, 3, c
='c')
221 ss
= dr
.SenderNoWx(preProc
, jobID
=321)
223 def subEnd2(chainTrav
, a
, c
=None):
227 assert chainTrav
.getJobID() == 321
228 res
= chainTrav
.get()
230 print 'Returning from subEnd2'
233 subStart1( dr
.Handler(handlerPP
, 1, a
='a') )
241 testStartWorkerEvent()