Skip to content

Commit a712643

Browse files
Berserker66black-sliver
authored andcommitted
Core: limit parallel APContainer writing (ArchipelagoMW#2443)
Co-authored-by: black-sliver <[email protected]>
1 parent 1b7336c commit a712643

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

worlds/Files.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
import json
44
import zipfile
5+
import os
6+
import threading
57

68
from typing import ClassVar, Dict, Tuple, Any, Optional, Union, BinaryIO
79

810
import bsdiff4
911

12+
semaphore = threading.Semaphore(os.cpu_count() or 4)
13+
14+
del threading
15+
del os
16+
1017

1118
class AutoPatchRegister(type):
1219
patch_types: ClassVar[Dict[str, AutoPatchRegister]] = {}
@@ -57,11 +64,12 @@ def write(self, file: Optional[Union[str, BinaryIO]] = None) -> None:
5764
zip_file = file if file else self.path
5865
if not zip_file:
5966
raise FileNotFoundError(f"Cannot write {self.__class__.__name__} due to no path provided.")
60-
with zipfile.ZipFile(zip_file, "w", self.compression_method, True, self.compression_level) \
61-
as zf:
62-
if file:
63-
self.path = zf.filename
64-
self.write_contents(zf)
67+
with semaphore: # TODO: remove semaphore once generate_output has a thread limit
68+
with zipfile.ZipFile(
69+
zip_file, "w", self.compression_method, True, self.compression_level) as zf:
70+
if file:
71+
self.path = zf.filename
72+
self.write_contents(zf)
6573

6674
def write_contents(self, opened_zipfile: zipfile.ZipFile) -> None:
6775
manifest = self.get_manifest()

0 commit comments

Comments
 (0)