Using select on a unix command in lieu of signal

rh0dium sklass at pointcircle.com
Mon Aug 29 17:13:21 EDT 2005


Paul Rubin wrote:
> "rh0dium" <sklass at pointcircle.com> writes:
> > Thanks much - Alternatively if anyone else has a better way to do what
> > I am trying to get done always looking for better ways.  I still want
> > this to work though..
>
> You don't have to use select, since you can use timeouts with normal
> socket i/o.  So you could use threads.  3000 threads is a lot but not
> insanely so.

OK I could use the timeout.. but I am using a queue as well.  So each
thread gets several commands.  I assumed (could be wrong) that if I use
a timeout the whole thread gets killed not the individual process.  The
purpose of the queue was to limit the number of concurrent workers, and
keep the load on the machine somewaht manageable.

So to add more to this here is how I call the runCmd

    # Initialize a que to 25 max hosts
    workQ = Queue.Queue(25)

    # Start some threads..
    for i in range(MAX_THREADS):
        getReachableHosts(queue=workQ).start()

    # Now give the threads something to do..  The nice thing here is
that by
    # waiting unil now this will hold up the queue..
    for host in base_hosts:
        workQ.put(host)

    # After this is finally done thow a null to close the threads..
    for i in range(MAX_THREADS):
        workQ.put(None)

And then getReachables..

class getReachableHosts(threading.Thread):
    def __init__(self,queue=None, ):
        self.logger = logging.getLogger("metriX.%s" %
self.__class__.__name__)
        self.logger.info("Initializing class %s" %
self.__class__.__name__)
        self.__queue = queue
        threading.Thread.__init__(self)

    def run(self):
        self.logger.debug("Initializing function %s" %
sys._getframe().f_code.co_name )
        while 1:
            host = self.__queue.get(timeout=5)
            if host is None:
                break

            self.logger.debug("Getting open ports on %s" % host)
            command = "nmap -p 22,514 -oG - %s | perl -lane 'print
unless /^#/'" % host

            (out,results)=self.runCmd(cmd=cmd,timeout=5)


Much appreciate the advice and help!!




More information about the Python-list mailing list