def test5(self): try: print timeout_yield(exc()) self.assert_(False, "should've raised an exception") except: pass like_yield(wait(1))
def test0(self): self.assert_(like_yield(f()) == 5, "should have returned 5.") self.assert_(like_yield(g(1)) == 6, "should have returned 6.") try: like_yield(exc()) self.assert_(False, "like_yield should've raised exception.") except Exception, e: pass
def test4(self): try: print timeout_yield(g(1), timeout=.5) self.assert_(False, "should've timed out.") except TimeoutError: pass like_yield(wait(1))
def test4(self): try: print timeout_yield(g(1), timeout=.5) self.assert_(False, "should've timed out." ) except TimeoutError: pass like_yield(wait(1))
def _genWoosh(self): x = like_yield(getThing()) self.assertEquals(x, "hi") try: like_yield(getOwie()) except ZeroDivisionError, e: self.assertEquals(str(e), 'OMG')
def test_rate_limited_logger(): injectLogger(verbose = True) log = RateLimitedLogger(logging.getLogger("myapp"), 1,1) log.info( "should be printed." ) log.info( "should not be printed" ) # but should log "discard" message. log.info( "also should not be printed" ) # should not logging of discard message. df = Deferred() reactor.callLater(3, df.callback, True) like_yield(df) log.info( "should also be printed" ) reactor.stop()
def init_yield(clss, *args, **kwargs): """Instantiate an object of type clss and then call its asynchronous initializer (__dfinit__). The __dfinit__ returns a Deferred. When the deferred's callback is called execution resumes at init_yield and the fully initialized object is returned.""" kwargs['__magic_init_yield'] = True obj = clss(*args, **kwargs) # synchronous initialization. like_yield(obj.__dfinit__()) # asynchronous initialization. # How it works: async_init returns a deferred. like_yield # installs callbacks in the deferred to the greenlet's switch. # When the deferred completes, it calls the greenlet's switch # causing execution to resume here. return obj
def run_tests(internal_port): list = like_yield(nattraverser.list_ports()) print_list(list) df = nattraverser.register_port(internal_port, internal_port, "TCP") external_port = like_yield(df) print "Mapped:", external_port list = like_yield(nattraverser.list_ports()) print_list(list) # synchronous nattraverser.unregister_port(external_port, "TCP") list = like_yield(nattraverser.list_ports()) print_list(list)
def timeout_yield(orig_df, timeout = None ): """like_yield with a timeout. Pased timeout is in seconds. If timeout is None then uses the default default_yield_timeout. If the function f eventually completes (i.e., its deferred gets called) after having already timed out then the result is tossed. timeout is set to None rather than default_yield_timeout so that the default can be changed after import timeout_yield by changing default_yield_timeout. WARNING: It is left to the caller to free up any state that might be held by the hung deferred. """ assert isinstance(orig_df, defer.Deferred) df = defer.Deferred() if timeout is None: timeout = default_yield_timeout t = reactor.callLater(timeout, defer.timeout, df) def good(r): if t.active(): df.callback(r) def bad(r): if t.active(): df.errback(r) orig_df.addCallbacks(good, bad) try: r = like_yield(df) finally: if t.active(): t.cancel() return r
def timeout_yield(orig_df, timeout=None): """like_yield with a timeout. Pased timeout is in seconds. If timeout is None then uses the default default_yield_timeout. If the function f eventually completes (i.e., its deferred gets called) after having already timed out then the result is tossed. timeout is set to None rather than default_yield_timeout so that the default can be changed after import timeout_yield by changing default_yield_timeout. WARNING: It is left to the caller to free up any state that might be held by the hung deferred. """ assert isinstance(orig_df, defer.Deferred) df = defer.Deferred() if timeout is None: timeout = default_yield_timeout t = reactor.callLater(timeout, defer.timeout, df) def good(r): if t.active(): df.callback(r) def bad(r): if t.active(): df.errback(r) orig_df.addCallbacks(good, bad) try: r = like_yield(df) finally: if t.active(): t.cancel() return r
def main(proxy): global logiter global all_lines global to_remove for url in logiter: try: peer_id = get_param(url, "peer_id") if peer_id: peer_id = id_map.setdefault(peer_id, make_id()) url = replace_param(url, "peer_id", peer_id) url = replace_param(url, "ip", "127.0.0.1") url = replace_param(url, "port", "6881") path = '/' + url.split('/', 3)[3] sys.stdout.write('.') df = proxy.callRemote(path) r = like_yield(df) r = bdecode(r) er = r.get('failure reason') if er: raise Exception(er) except Exception, e: print "Error", e.__class__, e, url to_remove.add(line) pass
def _genFailure(): x = defer.fail(TerminalException("Handled Terminal Failure")) try: like_yield(x) except TerminalException: pass
def _genDeferred(): like_yield(getThing())
def _genError(): like_yield(getThing()) 1/0
def test2(self): self.assert_(timeout_yield(f(), timeout=1) == 5, "with specified timeout should still return 5" ) like_yield(wait(1))
def _loop(): for x in range(5000): # Test with yielding a deferred like_yield(defer.succeed(1)) return 0
def g(t): sys.stdout.flush() like_yield(wait(t)) return 6
def test3(self): self.assert_(timeout_yield(g(.1), timeout=.5) == 6, "should return 6.") like_yield(wait(1))
def test2(self): self.assert_( timeout_yield(f(), timeout=1) == 5, "with specified timeout should still return 5") like_yield(wait(1))
def _genFailure(): try: like_yield(d) except TerminalException: pass
def test1(self): self.assert_(timeout_yield(f()) == 5, "should return 5" ) like_yield(wait(1))
def _genError(): like_yield(getThing()) 1 / 0
def test1(self): self.assert_(timeout_yield(f()) == 5, "should return 5") like_yield(wait(1))
def test3(self): self.assert_( timeout_yield(g(.1), timeout=.5) == 6, "should return 6." ) like_yield(wait(1))