def handleButtonClick(): produce = [123, 456, 789, 012, 345, 678, 901] expect = dr.Struct(idx=0) def worker(a, b=None, sender=None): assert a == 2 assert b == 'b' for val in produce: time.sleep(0.5) sender.sendResult(val) def consumer(result, b, a=None): assert b == 1 assert a == 'a' result = result.get() print 'got result', result if expect.idx < len(produce): assert result == produce[ expect.idx] #, 'Expected %s, got %s' % ( else: assert result is None app.ExitMainLoop() expect.idx += 1 dr.startWorker(consumer, worker, cargs=(1, ), ckwargs={'a': 'a'}, wargs=(2, ), wkwargs={'b': 'b'}, senderArg='sender')
def testThread(): expect = dr.Struct(value=1) def consumer(result): assert result.getJobID() is None assert result.get() == expect.value expect.value += 2 ss = dr.SenderNoWx(dr.Handler(consumer)) import time def worker(sender=None): sender.sendResult(1) time.sleep(0.1) sender.sendResult(3) time.sleep(0.1) sender.sendResult(5) time.sleep(0.1) return 7 tt = dr.Producer(ss, worker, senderArg='sender') tt.start() while expect.value < 7: time.sleep(0.1) print '.'
def testSender(): triplet = (1, 'a', 2.34) b = dr.Struct(called=False, which=1) assert not b.called def consumer(result, a, b=None): assert result.get() == 789 + b.which assert result.getJobID() == 456 assert a == 'a' b.called = True handler = dr.Handler(consumer, 'a', **dict(b=b)) ss = dr.SenderNoWx(handler, jobID=456) ss.sendResult(789 + 1) assert b.called
def testStartWorkerEvent(): print 'Doing same with events' import time produce = [123, 456, 789, 012, 345, 678, 901] expect = dr.Struct(idx=0) def worker(a, b=None, sender=None): assert a == 2 assert b == 'b' for val in produce: time.sleep(0.5) sender.sendResult(val) def consumer(event): assert event.a == 'a' result = event.result.get() print 'got result', result if expect.idx < len(produce): assert result == produce[expect.idx] #, 'Expected %s, got %s' % ( else: assert result is None app.ExitMainLoop() expect.idx += 1 def handleButtonClick(): dr.startWorker(frame, worker, cargs=(eventClass, ), ckwargs={ 'a': 'a', 'resultAttr': 'result' }, wargs=(2, ), wkwargs={'b': 'b'}, senderArg='sender') app = wx.PySimpleApp() frame = wx.Frame( None) # need this otherwise MainLoop() returns immediately from wx.lib.newevent import NewEvent as wxNewEvent eventClass, eventBinder = wxNewEvent() frame.Bind(eventBinder, consumer) # pretend user has clicked: import thread thread.start_new_thread(wx.CallAfter, (handleButtonClick, )) app.MainLoop()
def testStruct(): ss = dr.Struct(a='a', b='b') assert ss.a == 'a' assert ss.b == 'b'