Beispiel #1
0
class ThreadedResolverTest(AsyncTestCase, _ResolverTestMixin):
    def setUp(self):
        super(ThreadedResolverTest, self).setUp()
        self.resolver = ThreadedResolver(io_loop=self.io_loop)

    def tearDown(self):
        self.resolver.close()
        super(ThreadedResolverTest, self).tearDown()
Beispiel #2
0
class ThreadedResolverTest(AsyncTestCase, _ResolverTestMixin):
    def setUp(self):
        super(ThreadedResolverTest, self).setUp()
        self.resolver = ThreadedResolver(io_loop=self.io_loop)

    def tearDown(self):
        self.resolver.close()
        super(ThreadedResolverTest, self).tearDown()
def main():
    args = parse_command_line()

    if not args:
        args = ['localhost', 'www.google.com',
                'www.facebook.com', 'www.dropbox.com']

    resolvers = [Resolver(), ThreadedResolver()]

    if twisted is not None:
        from tornado.platform.twisted import TwistedResolver
        resolvers.append(TwistedResolver())

    if pycares is not None:
        from tornado.platform.caresresolver import CaresResolver
        resolvers.append(CaresResolver())

    family = {
        'unspec': socket.AF_UNSPEC,
        'inet': socket.AF_INET,
        'inet6': socket.AF_INET6,
    }[options.family]

    for host in args:
        print('Resolving %s' % host)
        for resolver in resolvers:
            addrinfo = yield resolver.resolve(host, 80, family)
            print('%s: %s' % (resolver.__class__.__name__,
                              pprint.pformat(addrinfo)))
        print()
Beispiel #4
0
def async_execute(fn):
    """
    新版tornado已有这个函数的实现,但是需要在class中绑定self.ioloop和self.executor,不太方便,这个版本消除了这个问题
    """
    thread_resolver = ThreadedResolver()

    @functools.wraps(fn)
    def wrapper(self, *args, **kwargs):
        callback = kwargs.pop("callback", None)
        future = thread_resolver.executor.submit(fn, self, *args, **kwargs)
        if callback:
            IOLoop.current().add_future(future,
                                    lambda future: callback(future.result()))
        return future
    return wrapper
Beispiel #5
0
 def setUp(self):
     super(ThreadedResolverTest, self).setUp()
     self.resolver = ThreadedResolver(io_loop=self.io_loop)
Beispiel #6
0
from tornado.ioloop import IOLoop
from tornado.netutil import ThreadedResolver

# When this module is imported, it runs getaddrinfo on a thread. Since
# the hostname is unicode, getaddrinfo attempts to import encodings.idna
# but blocks on the import lock. Verify that ThreadedResolver avoids
# this deadlock.

resolver = ThreadedResolver()
IOLoop.current().run_sync(lambda: resolver.resolve(u"localhost", 80))
Beispiel #7
0
 def setUp(self):
     super(ThreadedResolverTest, self).setUp()
     self.resolver = ThreadedResolver(io_loop=self.io_loop)
Beispiel #8
0
 def setUp(self):
     super(ThreadedResolverTest, self).setUp()
     self.resolver = ThreadedResolver()
Beispiel #9
0
 def wrapper(callback):
     thread_resolver = ThreadedResolver()
     fut = thread_resolver.executor.submit(method, *args, **kwargs)
     fut.add_done_callback(lambda future: IOLoop.current().add_callback(
         functools.partial(callback, future.result())))
Beispiel #10
0
 def setUp(self):
     super().setUp()
     self.resolver = ThreadedResolver()
from tornado.ioloop import IOLoop
from tornado.netutil import ThreadedResolver

# When this module is imported, it runs getaddrinfo on a thread. Since
# the hostname is unicode, getaddrinfo attempts to import encodings.idna
# but blocks on the import lock. Verify that ThreadedResolver avoids
# this deadlock.

resolver = ThreadedResolver()
IOLoop.current().run_sync(lambda: resolver.resolve(u"10.0.0.7", 80))
from tornado.ioloop import IOLoop
from tornado.netutil import ThreadedResolver

# When this module is imported, it runs getaddrinfo on a thread. Since
# the hostname is unicode, getaddrinfo attempts to import encodings.idna
# but blocks on the import lock. Verify that ThreadedResolver avoids
# this deadlock.

resolver = ThreadedResolver()
IOLoop.current().run_sync(lambda: resolver.resolve(u'localhost', 80))
Beispiel #13
0
 def _init_resolver(cls, num_threads=10):
     from tornado.netutil import ThreadedResolver
     cls._resolver = ThreadedResolver()
     cls._resolver.initialize(num_threads=num_threads)
Beispiel #14
0
 def __init__(self):
     self.executor = ThreadedResolver().executor
     self.io_loop = IOLoop.current()