Python 创建多线程执行多任务
线程的创建步骤:
- 导入线程模块
- 通过线程类创建线程对象
- 线程对象 = threading.Thread(target=任务名)
| 参数名 |
说明 |
| target |
执行的目标任务名,这里指的是函数名或方法名 |
| name |
进程名,一般不设置 |
| group |
进程组,目前只能使用None |
- 启动线程任务
线程创建和启动的代码
# 导入 threading 模块
inport threading
# 创建任务函数
def fun1(a):
print(a)
def func2(b):
print(b)
# 创建子线程1
thread_1 = threading.Thread(target=func1, args=(a, ))
# 创建子线程2
thread_2 = threading.Thread(target=func2, args=(b, ))
# 启动线程
thread_1.start()
thread_2.start()
# 等待子线程结束(可不写)
thread_1.join()
thread_2.join()
线程执行带有参数的任务
| 参数名 |
说明 |
| args |
以元组的方式给执行任务传参 |
| kwargs |
以字典方式给执行任务传参 |
# target: 线程执行函数名
# args:表示以元组的方式给函数传参
thread_1 = threading.Thread(target=function, args=(a, )) # a 表示传入的参数
thread_1.start()
# target: 线程执行函数名
# kwargs:表示以字典的方式给函数传参
thread_1 = threading.Thread(target=function, kwargs=("name":"xiaoming"))
thread_1.start()
示例代码
import threading
import requests
import time
urls = [f"https://www.cnblogs.com/#p{page}" for page in range(1, 51)]
def craw(url):
response = requests.get(url)
print(url, len(response.text))
# 创建也该单线程
def single_thread():
print("single_thread begin")
for url in urls:
craw(url)
print("single_thread end")
# 创建一个多线程
def multi_thread():
print("multi_thread begin")
threads = []
for url in urls:
threads.append(threading.Thread(target=craw, args=(url,)))
# 启动所有线程
for thread in threads:
thread.start()
# 等待所有线程结束
for thread in threads:
thread.join()
print("multi_thread end")
if __name__ == '__main__':
start = time.time()
single_thread()
end = time.time()
print("single thread cost: ", end - start, "seconds")
start = time.time()
multi_thread()
end = time.time()
print("multi thread cost: ", end - start, "seconds")