等待对象启动以返回列表,然后继续

时间:2015-01-22 22:25:19

标签: python multithreading class

我有一个主python脚本,它创建了两个对象

obj1 = xmlobj()
list1, list2, list3 = obj1.parsexml("xmlfile")
//parsexml returns me three lists
obj2 = htmlobj()
str1 = obj2.createhtmltable(list1, list2, list3)
//creates a html table

但是,当我运行脚本时,主脚本不会等待obj1.parsexml返回值,并执行并立即转到创建obj2的下一个语句,因此甚至不执行createhtmltable作为list1,list2,list3没有返回其中的价值观。另外,parsexml工作正常,没有任何错误

我该如何解决这个问题,谢谢

1 个答案:

答案 0 :(得分:0)

以下是一个例子:

import Queue  #python 3.x => import queue
import urllib2  #python 3.x => import urllib.request
import threading

urls = [
        "http://www.apple.com",
        "http://www.yahoo.com",
        "http://www.google.com",
        "http://www.espn.com",
]

def do_stuff():
    while True:
        url = task_q.get()

        if url == "NO_MORE_DATA":
            break

        data = urllib2.urlopen(url).read()[:200]
        results_q.put(data)

#Create the Queues:
task_q = Queue.Queue()
results_q = Queue.Queue()

num_worker_threads = 2

for i in range(num_worker_threads):
    t = threading.Thread(target=do_stuff)
    t.daemon = True  #daemon threads are killed when the main thread finishes
    t.start()  #All threads block because the task_q is empty.

#Add the tasks/urls to the task_q:
for url in urls:
    task_q.put(url)

#Like frantic dogs, all the threads are chomping on the task_q...

#Add a stop for each thread, so that after all the urls 
#have been removed from the task_q, the threads will terminate:
for i in range(num_worker_threads):   
    task_q.put("NO_MORE_DATA")  

results_count = len(urls)  #Need to know how many results are expected in order to know when to stop reading from the results_q

while True:
    result = results_q.get()

    #process the result here
    print result
    print '-' * 10

    results_count -= 1
    if results_count == 0:  #then all results have been processed, so you should stop trying to read from the results_q
        break
  

Queue.join()
  阻止直到队列中的所有项目都被获取并且   处理。

     

每当项目被添加到时,未完成任务的数量就会增加   队列。每当消费者线程调用时,计数都会下降   task_done()表示该项目已被检索并且所有工作都在其上   完成了。当未完成任务的数量降至零时,join()   疏导。   https://docs.python.org/2.7/library/queue.html#module-Queue

是的,但是谁想等到 all 结果已经添加到结果队列中,然后才开始处理结果。为什么不在第一个结果可用时立即开始处理?