Blog categories

Comments

[Python] multiprocessing 사용 하면서 file write 하기

[Python] multiprocessing 사용 하면서 file write 하기

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)

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

div#stuning-header .dfd-stuning-header-bg-container {background-image: url(https://tech.sangron.com/wp-content/uploads/sites/2/2018/02/python_wallpaper_background.jpg);background-color: #3f3f3f;background-size: cover;background-position: top center;background-attachment: initial;background-repeat: no-repeat;}#stuning-header div.page-title-inner {min-height: 350px;}