Python 에서 multiprocessing 을 이용하여 sub process에서 disk file write 하는 방법
from multiprocessing import Process, Queue END_TOKEN = 99999999 class mp_logger: def __init__(self, name, path): self.name = name self.path = path self.q = Queue() self.p = None self.join_timeout = 1 def start(self): self.p = Process(target=self.__listen) self.p.start() def __listen(self): with open( "{PATH}{NAME}.txt".format(PATH = self.path, NAME = self.name), "w" ) as output: while True: line = self.q.get() if line == END_TOKEN: break # Convert other object to text if not isinstance(line, str): line = str(line) output.write(str(line)) def close(self): assert(self.p != None) if self.p.is_alive(): self.q.put(END_TOKEN) self.p.join(self.join_timeout) def write(self, data): assert(self.p != None) self.q.put(data)