Code Monkey home page Code Monkey logo

Comments (4)

boytm avatar boytm commented on June 21, 2024

You can use asyncio instead of select.poll. In python 2.7, asyncio is named trollius .
https://github.com/saghul/pycares/blob/master/examples/cares-asyncio.py

from pycares.

thestick613 avatar thestick613 commented on June 21, 2024

I'm stuck on python2.7 and i also have to use gevent's monkey patch.

from pycares.

boytm avatar boytm commented on June 21, 2024

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()

from pycares.

saghul avatar saghul commented on June 21, 2024

Thanks for chiming in @boytm ! Closing this since an example was provided.

from pycares.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.