我正在尝试在pygtk中开发一个下载程序 因此,当用户在行动发生后添加网址
- addUrl()
调用
validateUrl()
getUrldetails()
因为urllib.urlopen
延迟,所以花了一点时间将网址添加到列表中
所以我试图实现线程。我将以下代码添加到主窗口
thread.start_new_thread(addUrl, (self,url, ))
我传递了对主窗口的引用,以便我可以从线程访问列表
但似乎没有任何事情发生
答案 0 :(得分:0)
我认为您先检查此帖子How to use threading in Python?。 例如: 导入队列 导入线程 import urllib2
# called by each thread
def get_url(q, url):
q.put(urllib2.urlopen(url).read())
theurls = '''http://google.com http://yahoo.com'''.split()
q = Queue.Queue()
for u in theurls:
t = threading.Thread(target=get_url, args = (q,u))
t.daemon = True
t.start()
s = q.get()
print s
希望这会对你有所帮助。