python 多线程的使用
2021/9/16 1:04:59
本文主要是介绍python 多线程的使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import threadpool url_list = ['https://www.hao123.com/1', 'https://www.hao123.com/2', 'https://www.hao123.com/3', 'https://www.hao123.com/4', 'https://www.hao123.com/5', 'https://www.hao123.com/6', 'https://www.hao123.com/7', 'https://www.hao123.com/8'] def get_data(url): print(url) pool = threadpool.ThreadPool(5) thread_list = threadpool.makeRequests(get_data, url_list) for i in thread_list: pool.putRequest(i) pool.wait()
import queue import threading url_list = ['https://www.hao123.com/1', 'https://www.hao123.com/2', 'https://www.hao123.com/3', 'https://www.hao123.com/4', 'https://www.hao123.com/5', 'https://www.hao123.com/6', 'https://www.hao123.com/7', 'https://www.hao123.com/8'] my_queue = queue.Queue(8) for i in url_list: my_queue.put(i) def fun1(): url = my_queue.get() print(url) def fun2(): url = my_queue.get() print(url) def fun3(): url = my_queue.get() print(url) t1 = threading.Thread(target=fun1) t2 = threading.Thread(target=fun2) t3 = threading.Thread(target=fun3) t1.start() t2.start() t3.start() t1.join() t2.join() t3.join()
这篇关于python 多线程的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享