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
+17 -9
View File
@@ -1,4 +1,5 @@
import threading
from multiprocessing import Process, Pipe
def background(func):
@@ -12,23 +13,30 @@ def 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__(
self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None
):
threading.Thread.__init__(self, group, target, name, args, kwargs)
self._return = None
Process.__init__(self, group=group, target=target, name=name, args=args, kwargs=kwargs)
self._parent_conn, self._child_conn = Pipe()
self._target = target
self._args = args
self._kwargs = kwargs
def run(self):
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):
threading.Thread.join(self, *args)
return self._return
Process.join(self, *args)
if self._parent_conn.poll():
return self._parent_conn.recv()
return None