def run(self, service, pub_id, file, send_to): self.watch(send_to) seek_ptr = 0 while True: try: msg = yield with_timeout(OPEN_FILE_TIMEOUT, self.get()) except Timeout: err("Sending of file at %r timed out" % (file,)) break if ('next-chunk', ANY) == msg: service << ('touch-file', pub_id) _, chunk_size = msg chunk = yield read_file_async(file, start=seek_ptr, end=seek_ptr + chunk_size) seek_ptr += len(chunk) more_coming = len(chunk) == chunk_size send_to << ('chunk', chunk, more_coming) if not more_coming: break elif ('terminated', send_to) == msg: break else: self.unhandled(msg)
def ask(self, q, request, arg=None, timeout=DEFAULT_ASK_TIMEOUT): d = with_timeout(5.0, Deferred()) self.actor << (q, d, arg) request.setHeader('Content-Type', 'text/plain') d.addCallback(lambda result: ( request.write( json.dumps({ 'success': True, 'result': result }, indent=4) + '\n'), request.finish(), )) d.addErrback(lambda f: ( request.write( json.dumps( { 'success': False, 'error': { 'traceback': f.getTraceback() } }, indent=4) + '\n'), request.finish(), )) return NOT_DONE_YET
def ask_html(self, q, template, request, arg=None, process=lambda x: x, timeout=DEFAULT_ASK_TIMEOUT): d = with_timeout(5.0, Deferred()) self.actor << (q, d, arg) # request.setHeader('Content-Type', 'text/plain') d.addCallback(lambda result: ( # TemplateLoader(os.path.join(os.path.dirname(__file__), 'templates'), auto_reload=True).load(template + '.html').generate(result=result).render('html', doctype='html') request.write( TemplateLoader(os.path.join(os.path.dirname(__file__), 'templates'), auto_reload=True).load(template + '.html') .generate(result=process(result)).render('html', doctype='html')), request.finish(), )) d.addErrback(lambda f: ( request.setHeader('Content-Type', 'text/plain'), request.write(f.getTraceback() + '\n'), request.finish(), )) return NOT_DONE_YET
def test_timeout_is_reached(clock): try: yield with_timeout(1.0, sleep(2.0, reactor=clock), reactor=clock) except Timeout: pass else: assert False, "Timeout should have been reached"
def run(self, other_actor): other_actor = lookup(other_actor) if isinstance(other_actor, str) else other_actor while True: dbg("sending greeting to %r" % (other_actor,)) other_actor << ('hello!', self.ref) dbg("waiting for ack from %r" % (other_actor,)) yield with_timeout(5.0, self.get('ack')) dbg("got 'ack' from %r; now sleeping a bit..." % (other_actor,)) yield sleep(1.0)
def run(self): child = self.spawn(ExampleActor) while True: dbg("sending greeting to %r" % (child,)) child << ('hello!', self.ref) dbg("waiting for ack from %r" % (child,)) yield with_timeout(5.0, self.get('ack')) dbg("got 'ack' from %r; now sleeping a bit..." % (child,)) yield sleep(1.0)
def ask(self, q, request, arg=None, timeout=DEFAULT_ASK_TIMEOUT): d = with_timeout(5.0, Deferred()) self.actor << (q, d, arg) request.setHeader('Content-Type', 'text/plain') d.addCallback(lambda result: ( request.write(json.dumps({'success': True, 'result': result}, indent=4) + '\n'), request.finish(), )) d.addErrback(lambda f: ( request.write(json.dumps({'success': False, 'error': {'traceback': f.getTraceback()}}, indent=4) + '\n'), request.finish(), )) return NOT_DONE_YET
def test_deferred_is_cancelled_when_timeout_reached(): clock = Clock() cancel_fn = MockFunction() mock = Deferred(canceller=cancel_fn) d = with_timeout(1.0, mock, reactor=clock) d.addErrback(lambda f: f.trap(CancelledError)) d.cancel() clock.advance(10) assert cancel_fn.called
def ask_html(self, q, template, request, arg=None, process=lambda x: x, timeout=DEFAULT_ASK_TIMEOUT): d = with_timeout(5.0, Deferred()) self.actor << (q, d, arg) # request.setHeader('Content-Type', 'text/plain') d.addCallback(lambda result: ( # TemplateLoader(os.path.join(os.path.dirname(__file__), 'templates'), auto_reload=True).load(template + '.html').generate(result=result).render('html', doctype='html') request.write(TemplateLoader(os.path.join(os.path.dirname(__file__), 'templates'), auto_reload=True).load(template + '.html').generate(result=process(result)).render('html', doctype='html')), request.finish(), )) d.addErrback(lambda f: ( request.setHeader('Content-Type', 'text/plain'), request.write(f.getTraceback() + '\n'), request.finish(), )) return NOT_DONE_YET
def expect(self, atleast=None, exactly=None, timeout=1.0): """If atleast > 1, returns `None`.""" assert bool(exactly is None) != bool(atleast is None), "exactly one of `atleast` or `exactly` is required" if atleast is None: atleast = exactly if exactly else 1 ret = [] for _ in range(atleast): if self.queue: ret.append(self.queue.pop(0)) else: self.d = Deferred() ret.append((yield with_timeout(timeout, self.d))) if exactly: yield self.expect_none() returnValue(ret)
def test_exceptions_are_passed_through(clock): class MyExc(Exception): pass @inlineCallbacks def dummy(): yield sleep(1.0, reactor=clock) raise MyExc try: yield with_timeout(2.0, dummy(), reactor=clock) except MyExc: pass except Timeout: assert False, "Should not have raised TimeoutError" else: assert False, "Should have raised MyExc"
def test_with_no_deferred(clock): foo = yield with_timeout(123, 'foo') assert foo == 'foo'
def test_with_no_timeout(): clock = Clock() d = with_timeout(None, Deferred(), clock) clock.advance(9999999999999999) assert not errback_called(d)
def test_timeout_is_not_reached(clock): try: yield with_timeout(2.0, sleep(1.0, clock), reactor=clock) except Timeout: assert False, "Timeout should not have been reached"