目前,我有一个url的列表来抓取内容,并正在做它连续。我想改变它并行抓住它们。这是一个伪代码。我想问的是设计的声音?我知道.start()启动线程,但是,我的数据库没有更新。我需要使用q.get()吗?谢谢帮助Python多线程
import threading
import Queue
q = Queue.Queue()
def do_database(url):
""" grab url then input to database """
webdata = grab_url(url)
try:
insert_data_into_database(webdata)
except:
....
else:
< do I need to do anything with the queue after each db operation is done?>
def put_queue(q, url):
q.put(do_database(url))
for myfiles in currentdir:
url = myfiles + some_other_string
t=threading.Thread(target=put_queue,args=(q,url))
t.daemon=True
t.start()
确保您在'insert_data_into_database'函数中提交事务,否则不会写入任何内容。 –
@Burhan Khalid,数据库工作正常,因为我提到我一直在串行。谢谢 – dorothy