Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def launch_coroutine(_f, *a, **kw):
    parent = greenlet.getcurrent()
    if isinstance(parent, GreenletWithDeferred):
        parent = parent.root
    df = defer.Deferred()
    g = GreenletWithDeferred(parent, df, _f, *a, **kw)
    g.switch()
    return df
Ejemplo n.º 3
0
def get_deferred_host_ips():
    global _host_ips
    global _host_ips_cachetime
    if hasattr(reactor, 'ident'):
        assert reactor.ident == thread.get_ident()

    if _host_ips is not None and _host_ips_cachetime + CACHE_TIME > bttime():
        return defer.succeed(_host_ips)

    df = get_deferred_host_ip()
    finaldf = defer.Deferred()
    df.addCallback(_get_deferred_host_ips2, finaldf)
    return finaldf
Ejemplo n.º 4
0
    def __init__(self,
                 external_port,
                 internal_port,
                 protocol,
                 host=None,
                 service_name=None,
                 remote_host=''):
        self.external_port = int(external_port)
        self.internal_port = int(internal_port)
        self.protocol = protocol

        self.host = host
        self.remote_host = ''

        self.service_name = service_name

        self.d = defer.Deferred()
Ejemplo n.º 5
0
def get_deferred_host_ip():
    global _host_ip
    global _host_ip_callbacks
    global _host_ip_cachetime
    if hasattr(reactor, 'ident'):
        assert reactor.ident == thread.get_ident()

    if _host_ip is not 'unknown' and _host_ip_cachetime + CACHE_TIME > bttime(
    ):
        return defer.succeed(_host_ip)

    if get_route_ip:
        ip = get_route_ip()
        if ip:
            _host_ip = ip
            _host_ip_cachetime = bttime()
            return defer.succeed(_host_ip)

    df = defer.Deferred()

    if not _host_ip_callbacks:

        def connect(ip):
            factory = RecorderFactory()
            factory.protocol = RecorderProtocol
            if hasattr(reactor, 'limiter'):
                reactor.connectTCP(ip, 80, factory, urgent=True)
            else:
                reactor.connectTCP(ip, 80, factory)

        rdf = reactor.resolve("ip.bittorrent.com")
        rdf.addCallback(connect)
        rdf.addErrback(lambda e: _got_result(None))

    _host_ip_callbacks.append(df)

    return df