subprocess.run执行cmd
This commit is contained in:
parent
42f055d5bf
commit
bb17302919
12
test_docker_run.py
Normal file
12
test_docker_run.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def run_docker_command(command):
|
||||||
|
result = subprocess.run(command, shell=True,
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
|
return result.stdout.decode('utf-8'), result.stderr.decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
command = "docker run -ti --rm -v g:/ODM_output/20241024100834/grid_1:/datasets opendronemap/odm --project-path /datasets project --max-concurrency 10 --force-gps --feature-quality lowest --orthophoto-resolution 10 --fast-orthophoto --skip-3dmodel --rerun-all"
|
||||||
|
stdout, stderr = run_docker_command(command)
|
||||||
|
print(stdout)
|
@ -1,17 +1,15 @@
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import docker
|
import subprocess
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
|
|
||||||
class ODMProcessMonitor:
|
class ODMProcessMonitor:
|
||||||
"""ODM进程监控器"""
|
"""ODM进程监控器"""
|
||||||
|
|
||||||
def __init__(self, max_retries: int = 3, mode: str = "快拼模式"):
|
def __init__(self, mode: str = "快拼模式"):
|
||||||
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是否执行成功"""
|
||||||
@ -21,49 +19,40 @@ 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命令"""
|
||||||
try:
|
try:
|
||||||
self.logger.info(f"开始处理网格 {grid_idx + 1}")
|
self.logger.info(f"开始处理网格 {grid_idx + 1}")
|
||||||
|
|
||||||
# 准备容器配置
|
# 构建命令字符串
|
||||||
volumes = {
|
command = (
|
||||||
grid_dir: {'bind': '/datasets', 'mode': 'rw'}
|
f"docker run -ti --rm "
|
||||||
}
|
f"-v {grid_dir}:/datasets "
|
||||||
|
f"opendronemap/odm "
|
||||||
# 准备命令参数
|
f"--project-path /datasets project "
|
||||||
command = [
|
f"--max-concurrency 10 "
|
||||||
"--project-path", "/datasets", "project",
|
f"--force-gps "
|
||||||
"--max-concurrency", "10",
|
|
||||||
"--force-gps",
|
|
||||||
"--rerun-all"
|
|
||||||
]
|
|
||||||
|
|
||||||
if fast_mode:
|
|
||||||
command.extend([
|
|
||||||
"--feature-quality", "lowest",
|
|
||||||
"--orthophoto-resolution", "8",
|
|
||||||
"--fast-orthophoto",
|
|
||||||
"--skip-3dmodel"
|
|
||||||
])
|
|
||||||
|
|
||||||
# 运行容器并等待完成
|
|
||||||
container = self.client.containers.run(
|
|
||||||
"opendronemap/odm",
|
|
||||||
command=command,
|
|
||||||
volumes=volumes,
|
|
||||||
detach=True,
|
|
||||||
remove=True,
|
|
||||||
environment={"PYTHONUNBUFFERED": "1"},
|
|
||||||
mem_limit="0", # 不限制内存
|
|
||||||
cpu_count=0, # 使用所有CPU
|
|
||||||
network_mode="host" # 使用主机网络模式
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# 等待容器完成并获取状态码
|
if fast_mode:
|
||||||
result = container.wait()
|
command += (
|
||||||
|
f"--feature-quality lowest "
|
||||||
|
f"--orthophoto-resolution 8 "
|
||||||
|
f"--fast-orthophoto "
|
||||||
|
f"--skip-3dmodel "
|
||||||
|
)
|
||||||
|
|
||||||
|
command += "--rerun-all"
|
||||||
|
|
||||||
|
# 执行命令
|
||||||
|
result = subprocess.run(
|
||||||
|
command,
|
||||||
|
shell=True,
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
stderr=subprocess.DEVNULL
|
||||||
|
)
|
||||||
|
|
||||||
# 检查是否成功完成
|
# 检查是否成功完成
|
||||||
if result['StatusCode'] == 0 and self._check_success(grid_dir):
|
if result.returncode == 0 and self._check_success(grid_dir):
|
||||||
self.logger.info(f"网格 {grid_idx + 1} 处理成功")
|
self.logger.info(f"网格 {grid_idx + 1} 处理成功")
|
||||||
return True, ""
|
return True, ""
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user