
为了解决这个问题,可以使用线程锁来确保在提取zip文件中的每个文件时,同一时间只有一个线程可以访问文件。这样可以避免多个线程同时访问和写入文件,从而解决race condition的问题。以下是修改后的示例代码:
```python
 import requests
 import threading
 import os
 import zipfile
def download_file(url, file_path):
     # 使用线程锁来确保同一时间只有一个线程可以访问文件
     lock = threading.Lock()
     with lock:
         # 发送GET请求从URL下载文件
         response = requests.get(url, stream=True)
         # 检查HTTP响应的状态码是否为200,200表示请求成功
         if response.status_code == 200:
             # 创建一个文件夹来保存文件
             if not os.path.exists(os.path.dirname(file_path)):
                 os.makedirs(os.path.dirname(file_path))
             # 创建一个zipfile对象来解压文件
             with zipfile.ZipFile(file_path, 'r') as zip_ref:
                 # 解压文件到指定的文件夹
                 zip_ref.extractall(os.path.dirname(file_path))
         else:
             print(f"Failed to download {file_path}")
# 使用线程池来并行下载多个文件
 with ThreadPoolExecutor(max_workers=5) as executor:
     urls = [
         'http://example.com/file1.zip',
         'http://example.com/file2.zip',
         'http://example.com/file3.zip',
         'http://example.com/file4.zip',
     ]
     file_paths = [
         'file1.zip',
         'file2.zip',
         'file3.zip',
         'file4.zip',
     ]
     executor.map(download_file, urls, file_paths)
 ```
在上述代码中,我们首先定义了一个名为`download_file`的函数,该函数接收一个URL和一个文件路径作为输入。