-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
any pycares example which uses poll instead of select? #63
Comments
You can use asyncio instead of select.poll. In python 2.7, asyncio is named trollius . |
I'm stuck on python2.7 and i also have to use gevent's monkey patch. |
adapted from pycares's pyuv and select example. The following example is also worked well for select.epoll, just replace self.poll = select.poll() by self.poll = select.epoll() code: import pycares
import select
import socket
class DNSResolver(object):
def __init__(self):
self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb, servers=['1.1.1.1'], timeout=5.0, flags=pycares.ARES_FLAG_USEVC)
#self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb)
self.poll = select.poll()
self._fd_map = set()
def _sock_state_cb(self, fd, readable, writable):
print "fd {} read {} write {}".format(fd, readable, writable)
if readable or writable:
event = (select.POLLIN if readable else 0) | (select.POLLOUT if writable else 0)
if fd not in self._fd_map:
# New socket
print "register %d" % fd
self.poll.register(fd, event)
self._fd_map.add(fd)
else:
print "modify %d" % fd
self.poll.modify(fd, event)
else:
# Socket is now closed
self._fd_map.remove(fd)
print "unregister %d" % fd
self.poll.unregister(fd)
def wait_channel(self):
while True:
if not self._fd_map:
break
timeout = self._channel.timeout(1.0)
if not timeout:
self._channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD)
continue
for fd, event in self.poll.poll(timeout):
if event & (select.POLLIN | select.POLLPRI):
self._channel.process_fd(fd, pycares.ARES_SOCKET_BAD)
if event & select.POLLOUT:
self._channel.process_fd(pycares.ARES_SOCKET_BAD, fd)
def query(self, query_type, name, cb):
self._channel.query(query_type, name, cb)
def gethostbyname(self, name, cb):
self._channel.gethostbyname(name, socket.AF_INET, cb)
if __name__ == '__main__':
def query_cb(result, error):
print result
print error
def gethostbyname_cb(result, error):
print result
print error
resolver = DNSResolver()
resolver.query('google.com', pycares.QUERY_TYPE_A, query_cb)
resolver.query('facebook.com', pycares.QUERY_TYPE_A, query_cb)
resolver.query('sip2sip.info', pycares.QUERY_TYPE_SOA, query_cb)
resolver.gethostbyname('apple.com', gethostbyname_cb)
resolver.wait_channel() |
Thanks for chiming in @boytm ! Closing this since an example was provided. |
I'm having trouble with select.select because of the 1024 file limit. How would a DNSResolver class which uses select.poll look like? Thank you.
The text was updated successfully, but these errors were encountered: