사용자 도구

사이트 도구


multiprocessing

multiprocessing

  • 자식프로세스 출력 redirection
import app
import io
import sys
from multiprocessing import Process
 
def run_app(some_param):
    sys.stdout = io.TextIOWrapper(open(sys.stdout.fileno(), 'wb', 0), write_through=True)
    app.run()
 
app_process = Process(target=run_app, args=('some_param',))
app_process.start()
# Use app_process.termninate() for python <= 3.7.
app_process.kill() 
 
# https://stackoverflow.com/questions/7714868/python-multiprocessing-how-can-i-reliably-redirect-stdout-from-a-child-process
original_stdout = sys.stdout
buffer = deque(maxlen=128)
 
class IOLoggerWrapper(io.StringIO):
    def write(self, string):
        buffer.append(string)
        return len(string)
 
root = logging.getLogger()
handler = logging.StreamHandler(IOLoggerWrapper())
formatter = logging.Formatter("[%(asctime)s][%(name)s][%(levelname)s][%(message)s]")
handler.setFormatter(formatter)
root.addHander(handler)
 
class IOWrapper(io.StringIO):
    def write(self, string):
        original_stdout.write(string)
        buffer.append(string)
        return len(string)
 
io_wrapper = IOWrapper()
sys.stdout = io_wrapper
sys.stderr = io_wrapper

multiprocessing 대체 모듈

multiprocessing.txt · 마지막으로 수정됨: 2024/03/23 02:38 저자 127.0.0.1