mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:38:37 +00:00
Merge pull request #2253 from kirigayakazima/codex/batch-glb-bounded-model-queue
batch_glb: bound queued model conversions
This commit is contained in:
@ -14,6 +14,7 @@ import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
TOOLS = os.path.join(ROOT, "tools")
|
||||
@ -151,6 +152,37 @@ class TestBatchGlb(unittest.TestCase):
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertIn("converted 1/1 models", result.stdout)
|
||||
|
||||
def test_bounds_pending_models_and_preserves_report_order(self) -> None:
|
||||
futures = batch_glb.concurrent.futures
|
||||
outstanding = 0
|
||||
peak = 0
|
||||
|
||||
class CountedFuture(futures.Future):
|
||||
def result(self, timeout=None):
|
||||
nonlocal outstanding
|
||||
outstanding -= 1
|
||||
return super().result(timeout)
|
||||
|
||||
class Pool(futures.Executor):
|
||||
def submit(self, fn, task):
|
||||
nonlocal outstanding, peak
|
||||
outstanding += 1
|
||||
peak = max(peak, outstanding)
|
||||
future = CountedFuture()
|
||||
future.set_result((False, task[0], "error", [("texture", "reason")]))
|
||||
return future
|
||||
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
for i in range(25):
|
||||
(Path(root) / f"{i:02}.obj").touch()
|
||||
order = [os.path.join(root, name) for name in os.listdir(root)]
|
||||
with mock.patch.object(futures, "ThreadPoolExecutor", return_value=Pool()):
|
||||
stats = batch_glb.batch_convert(root, root, jobs=2)
|
||||
self.assertLessEqual(peak, 4)
|
||||
self.assertEqual(stats["failed"], len(order))
|
||||
self.assertEqual([path for path, _ in stats["failures"]], order)
|
||||
self.assertEqual([path for path, _, _ in stats["dropped_textures"]], order)
|
||||
|
||||
def test_batch_convert_synthetic(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as in_dir, tempfile.TemporaryDirectory() as out_dir:
|
||||
for i in range(3):
|
||||
|
||||
@ -213,7 +213,7 @@ def batch_convert(
|
||||
tex_index = _build_texture_index(texture_dir)
|
||||
tex_cache = TextureCache()
|
||||
|
||||
tasks = [
|
||||
tasks = (
|
||||
(
|
||||
os.path.join(in_dir, f),
|
||||
os.path.join(out_dir, os.path.splitext(f)[0] + ".glb"),
|
||||
@ -223,29 +223,43 @@ def batch_convert(
|
||||
tex_cache,
|
||||
)
|
||||
for f in obj_files
|
||||
]
|
||||
|
||||
total = len(tasks)
|
||||
succeeded = 0
|
||||
failed = 0
|
||||
failures: list[tuple[str, str]] = [] # (model, error): counted and named
|
||||
dropped_textures: list[tuple[str, str, str]] = []
|
||||
)
|
||||
|
||||
total = len(obj_files)
|
||||
failures = []
|
||||
dropped_textures = []
|
||||
reports = {}
|
||||
max_workers = jobs if jobs and jobs > 0 else (os.cpu_count() or 4)
|
||||
|
||||
remaining = enumerate(tasks)
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||
for ok, path, err, dropped in executor.map(convert_single, tasks):
|
||||
if ok:
|
||||
succeeded += 1
|
||||
else:
|
||||
failed += 1
|
||||
failures.append((path, err))
|
||||
dropped_textures.extend((path, tex, why) for tex, why in dropped)
|
||||
pending = {}
|
||||
while True:
|
||||
for _ in range(2 * max_workers - len(pending)):
|
||||
item = next(remaining, None)
|
||||
if item is None:
|
||||
break
|
||||
index, task = item
|
||||
pending[executor.submit(convert_single, task)] = index
|
||||
if not pending:
|
||||
break
|
||||
ready, _ = concurrent.futures.wait(pending, return_when=concurrent.futures.FIRST_COMPLETED)
|
||||
for future in ready:
|
||||
index = pending.pop(future)
|
||||
ok, path, err, dropped = future.result()
|
||||
if not ok or dropped:
|
||||
reports[index] = (ok, path, err, dropped)
|
||||
|
||||
# Workers finish out of order. Keep CLI diagnostics in input order.
|
||||
for index in sorted(reports):
|
||||
ok, path, err, dropped = reports[index]
|
||||
if not ok:
|
||||
failures.append((path, err))
|
||||
dropped_textures.extend((path, tex, why) for tex, why in dropped)
|
||||
|
||||
return {
|
||||
"total": total,
|
||||
"succeeded": succeeded,
|
||||
"failed": failed,
|
||||
"succeeded": total - len(failures),
|
||||
"failed": len(failures),
|
||||
"failures": failures,
|
||||
"dropped_textures": dropped_textures,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user