雖然我搜索了很多結果,但他們不是我想要的。我的主要代碼如下如何等待所有線程完成?
def main:
start = datetime.now()
browser = webdriver.PhantomJS()
download()
browser.quit()
showTime()
def download:
for imageSecond in imageSeconds:
urlServer = imageSecond.get("src")
pathLocal = formatPath(downloadLocationPath, ntpath.basename(urlServer))
if not os.path.isfile(pathLocal):
ts.append(createNewDownloadThread(browser, urlServer, pathLocal))
else:
logger.info('Downloaded: {}'.format(urlServer + " -> " + pathLocal))
showTime()
for t in ts:
t.join()
def showTime:
end = datetime.now()
runtime = end - start
logger.info('Sta Time: {}'.format(start))
logger.info('End Time: {}'.format(end))
logger.info('Run Time: {}'.format(runtime))
sys.exit(0)
我得到的輸出如下
2017-02-27 09:42:12,817 - INFO - MainThread - Downloaded: https://secure-api.userlocal.jp
2017-02-27 09:42:12,833 - INFO - MainThread - Sta Time: 2017-02-27 09:41:43.895126
2017-02-27 09:42:12,833 - INFO - MainThread - End Time: 2017-02-27 09:42:12.833492
2017-02-27 09:42:12,833 - INFO - MainThread - Run Time: 0:00:28.938366
2017-02-27 09:42:12,849 - INFO - Thread-323 - Download: https://secure-api.userlocal.jp
2017-02-27 09:42:12,849 - INFO - Thread-324 - Download: https://secure-api.userlocal.jp
但我想如下輸出什麼,我該怎麼辦?
2017-02-27 09:42:12,817 - INFO - MainThread - Downloaded: https://secure-api.userlocal.jp
2017-02-27 09:42:12,849 - INFO - Thread-323 - Download: https://secure-api.userlocal.jp
2017-02-27 09:42:12,849 - INFO - Thread-324 - Download: https://secure-api.userlocal.jp
2017-02-27 09:42:12,833 - INFO - MainThread - Sta Time: 2017-02-27 09:41:43.895126
2017-02-27 09:42:12,833 - INFO - MainThread - End Time: 2017-02-27 09:42:12.833492
2017-02-27 09:42:12,833 - INFO - MainThread - Run Time: 0:00:28.938366
這可能會幫助:http://stackoverflow.com/questions/15085348/what-is-the-use- python-threading – EvanL00