====== 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 대체 모듈 =====
* Uber Fiber
* https://uber.github.io/fiber/
* multiprocessing과 사용법이 동일한 분산 컴퓨팅
* loky: resuable pool executer
* https://pypi.org/project/loky/
* Array 공유
* https://pypi.org/project/SharedArray/