subprocess.run执行命令
This commit is contained in:
parent
cdea54e77a
commit
bbb36d9814
@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
import docker
|
import subprocess
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
|
|
||||||
@ -12,7 +12,6 @@ class ODMProcessMonitor:
|
|||||||
self.max_retries = max_retries
|
self.max_retries = max_retries
|
||||||
self.logger = logging.getLogger('UAV_Preprocess.ODMMonitor')
|
self.logger = logging.getLogger('UAV_Preprocess.ODMMonitor')
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.client = docker.from_env()
|
|
||||||
|
|
||||||
def _check_success(self, grid_dir: str) -> bool:
|
def _check_success(self, grid_dir: str) -> bool:
|
||||||
"""检查ODM是否执行成功"""
|
"""检查ODM是否执行成功"""
|
||||||
@ -22,57 +21,48 @@ class ODMProcessMonitor:
|
|||||||
return all(os.path.exists(os.path.join(grid_dir, 'project', marker)) for marker in success_markers)
|
return all(os.path.exists(os.path.join(grid_dir, 'project', marker)) for marker in success_markers)
|
||||||
|
|
||||||
def run_odm_with_monitor(self, grid_dir: str, grid_idx: int, fast_mode: bool = True) -> Tuple[bool, str]:
|
def run_odm_with_monitor(self, grid_dir: str, grid_idx: int, fast_mode: bool = True) -> Tuple[bool, str]:
|
||||||
"""运行ODM容器"""
|
"""运行ODM命令"""
|
||||||
attempt = 0
|
attempt = 0
|
||||||
while attempt < self.max_retries:
|
while attempt < self.max_retries:
|
||||||
try:
|
try:
|
||||||
self.logger.info(f"网格 {grid_idx + 1} 第 {attempt + 1} 次尝试")
|
self.logger.info(f"网格 {grid_idx + 1} 第 {attempt + 1} 次尝试")
|
||||||
|
|
||||||
# 准备容器配置
|
# 构建基础命令
|
||||||
volumes = {
|
cmd = [
|
||||||
grid_dir: {'bind': '/datasets', 'mode': 'rw'}
|
"docker", "run", "--rm",
|
||||||
}
|
"-v", f"{grid_dir}:/datasets",
|
||||||
|
"opendronemap/odm",
|
||||||
# 准备命令参数
|
|
||||||
command = [
|
|
||||||
"--project-path", "/datasets", "project",
|
"--project-path", "/datasets", "project",
|
||||||
"--max-concurrency", "10",
|
"--max-concurrency", "10",
|
||||||
"--force-gps",
|
"--force-gps",
|
||||||
"--rerun-all"
|
"--rerun-all"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# 快速模式额外参数
|
||||||
if fast_mode:
|
if fast_mode:
|
||||||
command.extend([
|
cmd.extend([
|
||||||
"--feature-quality", "lowest",
|
"--feature-quality", "lowest",
|
||||||
"--orthophoto-resolution", "8",
|
"--orthophoto-resolution", "8",
|
||||||
"--fast-orthophoto",
|
"--fast-orthophoto",
|
||||||
"--skip-3dmodel"
|
"--skip-3dmodel"
|
||||||
])
|
])
|
||||||
|
|
||||||
# 运行容器
|
# 执行命令,不捕获输出
|
||||||
container = self.client.containers.run(
|
result = subprocess.run(
|
||||||
"opendronemap/odm",
|
cmd,
|
||||||
command=command,
|
stdout=subprocess.DEVNULL,
|
||||||
volumes=volumes,
|
stderr=subprocess.PIPE,
|
||||||
detach=True,
|
text=True
|
||||||
remove=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# 等待容器完成
|
# 检查执行结果
|
||||||
result = container.wait()
|
if result.returncode == 0 and self._check_success(grid_dir):
|
||||||
|
|
||||||
# 只在失败时获取日志
|
|
||||||
if result['StatusCode'] != 0:
|
|
||||||
logs = container.logs().decode('utf-8')
|
|
||||||
self.logger.error("容器执行失败,最后10行日志:")
|
|
||||||
self.logger.error(''.join(logs.split('\n')[-10:]))
|
|
||||||
|
|
||||||
# 检查是否成功完成
|
|
||||||
if result['StatusCode'] == 0 and self._check_success(grid_dir):
|
|
||||||
self.logger.info(f"网格 {grid_idx + 1} ODM处理成功")
|
self.logger.info(f"网格 {grid_idx + 1} ODM处理成功")
|
||||||
return True, ""
|
return True, ""
|
||||||
|
|
||||||
self.logger.warning(f"网格 {grid_idx + 1} 第 {attempt + 1} 次尝试失败")
|
# 只在失败时记录错误信息
|
||||||
|
if result.returncode != 0:
|
||||||
|
self.logger.error(f"网格 {grid_idx + 1} 执行失败,错误信息:{result.stderr[-500:]}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_msg = f"执行异常: {str(e)}"
|
error_msg = f"执行异常: {str(e)}"
|
||||||
|
Loading…
Reference in New Issue
Block a user