Python队列问题队列.get不会移除目标

2024-09-30 20:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个线程化python脚本,它将一些主机/ip放入队列,然后未知数量的线程(默认值为10)从队列中获取这些主机并执行snmp查询。Snmp查询正在工作,但每个线程都在查询所有主机。 主机只想从随机线程接收单个调用。在这里,我从每个线程得到对主机的调用。在

#!/usr/bin/env python
import Queue
import threading
import urllib2
import time
import datetime
import netsnmp
import pprint

queue = Queue.Queue()

class ThreadUrl(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def run(self):
        while True:
      #grabs host from queue
            item = self.queue.get()
            print item

      #grabs urls of hosts and prints first 1024 bytes of page
            dane = get_rittal(item)
            obj_in_que = self.queue.qsize()
            #print obj_in_que 
            print dane + "\n"

      #signals to queue job is done
            self.queue.task_done()

def log(message):
    now = datetime.datetime.now().strftime("%H:%M:%S")
    print "%s %s\n" % (now, message)



def convert(val,div):
    string = str(val)
    f = '%05.2f' % (float(string)*div)
    return f

def get_rittal(rittal):
    x = netsnmp.Session(Version=1,DestHost=rittal,Community='',Timeout=25000, Retries=3)
    oid_loadA = netsnmp.Varbind('.1.3.6.1.4.1.2606.100.1.2.2.1.7.1',1)
    oid_loadB = netsnmp.Varbind('.1.3.6.1.4.1.2606.100.1.2.2.1.7.1',2)
    oid_loadC = netsnmp.Varbind('.1.3.6.1.4.1.2606.100.1.2.2.1.7.1',3)

    all_oids = netsnmp.VarList(oid_loadA, oid_loadB, oid_loadC)

    get = list(x.get(all_oids))

    loadA = convert(get[0],0.01)
    loadB = convert(get[1],0.01)
    loadC = convert(get[2],0.01)

    res = 'Host:' + rittal + '\t\tLA:' + loadA + 'A LB:' + loadB + 'A LC:' + loadC + 'A'
    return res

start = time.time()
def main():
    list=[]
    for line in open ("test", "r").readlines():
        for h in line.rstrip('\n').split(' '):
            #print h
            list.append(h)
    x = str(len(list))
    print "Ilosc rittali = " + x
    print list;

  #spawn a pool of threads, and pass them queue instance 
    for i in range(10):
        t = ThreadUrl(queue)
        t.setDaemon(True)
        t.start()

 #populate queue with data   
        for item in list:
            queue.put(item)

 #wait on the queue until everything has been processed     
        queue.join()

main()
print "Elapsed Time: %s" % (time.time() - start)

样本输出:

^{pr2}$

Tags: inimportselfconvertgettimequeuedef
1条回答
网友
1楼 · 发布于 2024-09-30 20:32:51

仔细检查你的压痕。在

每次初始化一个新的工作线程时,您都会将主机列表的整个列表排队。此外,通过join()对队列进行序列化:

for i in xrange(10):
  t = ThreadURL(q)
  ...
  for item in host_list:   # oops
    q.put(item)
  q.join()                 # oops

相关问题 更多 >