如何在python中访问各种线程的函数

时间:2014-02-12 12:36:49

标签: python multithreading thread-safety threadpool

我在Windows上使用python 2.7。我在调用线程。在线程类我使用的是一个函数。

 hosts = ["https://www.onlinesbi.com/", "https://anking.syndicatebank.in/netbanking/", "https://netbanking.hdfcbank.com/netbanking/",
    "https://www.onlineandhrabank.net.in/", "https://netbanking.canarabank.in/netbanking/RetailLogin.html", "https://www.icicicareers.com/icici_career/probationary-recruitment.html"]
    success=[]
    queue = Queue.Queue()
    class ThreadUrl(threading.Thread):
      """Threaded Url Grab"""
      def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
      def run(self):
        try:  
            while True:
              #grabs host from queue
              host = self.queue.get()

          # (!) The below call produces the error (!)
          rsp,responsecode = myfunction(host, "GET", False, False)
          if responsecode ==200:
            success.append(host)
            print rsp
            print "----------------------------------%%%%%------"

          #signals to queue job is done
          self.queue.task_done()
    except Exception as e:
           self.queue.task_done()
           print e
start = time.time()
def main():
    try:
  #spawn a pool of threads, and pass them queue instance 
      for i in range(3):
        t = ThreadUrl(queue)
        t.setDaemon(True)
        t.start()

      #populate queue with data   
      for host in hosts:
        queue.put(host)

      #wait on the queue until everything has been processed     
      queue.join()
    except Exception as e:
           print e
main()
print success
print "Elapsed Time: %s" % (time.time() - start)**

myfunction(主机," GET",False,False)中,我想在每个帖子中使用,但我收到的错误是&#39 ;模块'对象不可调用且进程未终止

0 个答案:

没有答案