Python 多线程、日期时间处理与 XML 解析
1. 多线程模块的使用
在 Python 中,多线程是一种提高程序效率的有效方式,特别是在处理需要长时间运行的任务时,如文件下载或数据库查询。以下是一个使用多线程进行文件下载的示例代码:
# create a thread pool and give them a queue
for i in range(5):
t = Downloader(queue)
t.setDaemon(True)
t.start()
# give the queue some data
for url in urls:
queue.put(url)
# wait for the queue to finish
queue.join()
if __name__ == "__main__":
urls = ["http://www.irs.gov/pub/irs-pdf/f1040.pdf",
"http://www.irs.gov/pub/irs-pdf/f1040a.pdf",
"http://www.irs.gov/pub/irs-pdf/f1040ez.pdf",
"http://www.irs.gov/pub/irs-pdf/f1040es.pdf",
"http://www.irs.gov/pub/irs-pdf/f1040sb.pdf"]
main(urls)
上述代码的执行步骤如下:
1. 创
超级会员免费看
订阅专栏 解锁全文

1171

被折叠的 条评论
为什么被折叠?



