process silence with processes instead of threads

- might help memory issues?
+ fix double slash problem on windows?
This commit is contained in:
cwilvx
2025-03-13 18:32:52 +03:00
parent 5c7d84396d
commit 561d70352f
3 changed files with 23 additions and 16 deletions
+3 -4
View File
@@ -99,11 +99,12 @@ def get_folder_tree(body: FolderTree):
if is_windows(): if is_windows():
# Trailing slash needed when drive letters are passed, # Trailing slash needed when drive letters are passed,
# Remember, the trailing slash is removed in the client. # Remember, the trailing slash is removed in the client.
req_dir += "/" # req_dir += "/"
pass
else: else:
req_dir = "/" + req_dir if not req_dir.startswith("/") else req_dir req_dir = "/" + req_dir if not req_dir.startswith("/") else req_dir
res = get_files_and_dirs( return get_files_and_dirs(
req_dir, req_dir,
start=body.start, start=body.start,
limit=body.limit, limit=body.limit,
@@ -114,8 +115,6 @@ def get_folder_tree(body: FolderTree):
foldersort_reverse=body.foldersort_reverse, foldersort_reverse=body.foldersort_reverse,
) )
return res
def get_all_drives(is_win: bool = False): def get_all_drives(is_win: bool = False):
""" """
+3 -3
View File
@@ -6,7 +6,7 @@ import os
from app.lib.pydub.pydub import AudioSegment from app.lib.pydub.pydub import AudioSegment
from app.lib.pydub.pydub.silence import detect_leading_silence, detect_silence from app.lib.pydub.pydub.silence import detect_leading_silence, detect_silence
from app.utils.threading import ThreadWithReturnValue from app.utils.threading import ProcessWithReturnValue
def get_leading_silence_end(filepath: str): def get_leading_silence_end(filepath: str):
@@ -58,13 +58,13 @@ def get_silence_paddings(ending_file: str, starting_file: str):
starting_thread = None starting_thread = None
if os.path.exists(ending_file): if os.path.exists(ending_file):
ending_thread = ThreadWithReturnValue( ending_thread = ProcessWithReturnValue(
target=get_trailing_silence_start, args=(ending_file,) target=get_trailing_silence_start, args=(ending_file,)
) )
ending_thread.start() ending_thread.start()
if os.path.exists(starting_file): if os.path.exists(starting_file):
starting_thread = ThreadWithReturnValue( starting_thread = ProcessWithReturnValue(
target=get_leading_silence_end, args=(starting_file,) target=get_leading_silence_end, args=(starting_file,)
) )
starting_thread.start() starting_thread.start()
+17 -9
View File
@@ -1,4 +1,5 @@
import threading import threading
from multiprocessing import Process, Pipe
def background(func): def background(func):
@@ -12,23 +13,30 @@ def background(func):
return background_func return background_func
class ThreadWithReturnValue(threading.Thread):
"""
A thread class that returns a value on join.
Credit: https://stackoverflow.com/a/6894023 class ProcessWithReturnValue(Process):
"""
A process class that returns a value on join.
Uses a pipe to communicate the return value back to the parent process.
""" """
def __init__( def __init__(
self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None
): ):
threading.Thread.__init__(self, group, target, name, args, kwargs) Process.__init__(self, group=group, target=target, name=name, args=args, kwargs=kwargs)
self._return = None self._parent_conn, self._child_conn = Pipe()
self._target = target
self._args = args
self._kwargs = kwargs
def run(self): def run(self):
if self._target is not None: if self._target is not None:
self._return = self._target(*self._args, **self._kwargs) result = self._target(*self._args, **self._kwargs)
self._child_conn.send(result)
self._child_conn.close()
def join(self, *args): def join(self, *args):
threading.Thread.join(self, *args) Process.join(self, *args)
return self._return if self._parent_conn.poll():
return self._parent_conn.recv()
return None