mirror of
https://github.com/g0ldyy/comet.git
synced 2026-01-12 01:16:12 +01:00
perf: reduce RTN parsing chunk size to 20 and rework ProcessPoolExecutor system
This commit is contained in:
+28
-3
@@ -1,17 +1,42 @@
|
||||
import atexit
|
||||
import multiprocessing
|
||||
import os
|
||||
import signal
|
||||
from concurrent.futures import ProcessPoolExecutor
|
||||
|
||||
_mp_context = None
|
||||
try:
|
||||
_mp_context = multiprocessing.get_context("forkserver")
|
||||
except ValueError:
|
||||
_mp_context = multiprocessing.get_context("spawn")
|
||||
|
||||
app_executor = None
|
||||
|
||||
|
||||
def setup_executor():
|
||||
def worker_initializer():
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
|
||||
|
||||
def setup_executor(max_workers: int | None = None):
|
||||
global app_executor
|
||||
app_executor = ProcessPoolExecutor()
|
||||
|
||||
if max_workers is None:
|
||||
cpu_count = os.cpu_count() or 1
|
||||
max_workers = min(cpu_count, 4)
|
||||
|
||||
app_executor = ProcessPoolExecutor(
|
||||
max_workers=max_workers, mp_context=_mp_context, initializer=worker_initializer
|
||||
)
|
||||
|
||||
|
||||
def shutdown_executor():
|
||||
global app_executor
|
||||
if app_executor:
|
||||
app_executor.shutdown()
|
||||
app_executor.shutdown(wait=True, cancel_futures=True)
|
||||
app_executor = None
|
||||
|
||||
|
||||
atexit.register(shutdown_executor)
|
||||
|
||||
|
||||
def get_executor():
|
||||
|
||||
@@ -172,7 +172,7 @@ class TorrentManager:
|
||||
return
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
chunk_size = 50
|
||||
chunk_size = 20
|
||||
tasks = [
|
||||
loop.run_in_executor(
|
||||
get_executor(),
|
||||
|
||||
Reference in New Issue
Block a user