resolver_thread.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) 2012-2015 Denis Bilenko. See LICENSE for details.
  2. """
  3. Native thread-based hostname resolver.
  4. """
  5. import _socket
  6. from gevent._compat import text_type
  7. from gevent.hub import get_hub
  8. __all__ = ['Resolver']
  9. # trigger import of encodings.idna to avoid https://github.com/gevent/gevent/issues/349
  10. text_type('foo').encode('idna')
  11. class Resolver(object):
  12. """
  13. Implementation of the resolver API using native threads and native resolution
  14. functions.
  15. Using the native resolution mechanisms ensures the highest
  16. compatibility with what a non-gevent program would return
  17. including good support for platform specific configuration
  18. mechanisms. The use of native (non-greenlet) threads ensures that
  19. a caller doesn't block other greenlets.
  20. This implementation also has the benefit of being very simple in comparison to
  21. :class:`gevent.resolver_ares.Resolver`.
  22. .. tip::
  23. Most users find this resolver to be quite reliable in a
  24. properly monkey-patched environment. However, there have been
  25. some reports of long delays, slow performance or even hangs,
  26. particularly in long-lived programs that make many, many DNS
  27. requests. If you suspect that may be happening to you, try the
  28. ares resolver (and submit a bug report).
  29. """
  30. def __init__(self, hub=None):
  31. if hub is None:
  32. hub = get_hub()
  33. self.pool = hub.threadpool
  34. if _socket.gaierror not in hub.NOT_ERROR:
  35. # Do not cause lookup failures to get printed by the default
  36. # error handler. This can be very noisy.
  37. hub.NOT_ERROR += (_socket.gaierror, _socket.herror)
  38. def __repr__(self):
  39. return '<gevent.resolver_thread.Resolver at 0x%x pool=%r>' % (id(self), self.pool)
  40. def close(self):
  41. pass
  42. # from briefly reading socketmodule.c, it seems that all of the functions
  43. # below are thread-safe in Python, even if they are not thread-safe in C.
  44. def gethostbyname(self, *args):
  45. return self.pool.apply(_socket.gethostbyname, args)
  46. def gethostbyname_ex(self, *args):
  47. return self.pool.apply(_socket.gethostbyname_ex, args)
  48. def getaddrinfo(self, *args, **kwargs):
  49. return self.pool.apply(_socket.getaddrinfo, args, kwargs)
  50. def gethostbyaddr(self, *args, **kwargs):
  51. return self.pool.apply(_socket.gethostbyaddr, args, kwargs)
  52. def getnameinfo(self, *args, **kwargs):
  53. return self.pool.apply(_socket.getnameinfo, args, kwargs)