Sunday, 20 December 2015

Python: multiprocessing: Get the logger used by multiprocessing


‘multiprocessing.get_logger()’ return the logger used by multiprocessing. If necessary, a new one will be created. Be default the logger has level logging.NOTSET and no default handler.

from multiprocessing import Process
import multiprocessing
import logging

def print_data():
    name = multiprocessing.current_process().name
    id = multiprocessing.current_process().pid
    print(name, id)
    print("Processing finished")

if __name__=="__main__":
    multiprocessing.log_to_stderr(logging.DEBUG)
    logger = multiprocessing.get_logger()

    logger.debug("Started logging data")

    proc1 = Process(name='process1', target=print_data)

    proc1.start()
    proc1.join()

    print("Is proc1 alive ",proc1.is_alive())


    print("Finished")


Output
[DEBUG/MainProcess] Started logging data
[INFO/process1] child process calling self.run()
[INFO/process1] process shutting down
[DEBUG/process1] running all "atexit" finalizers with priority >= 0
[DEBUG/process1] running the remaining "atexit" finalizers
[INFO/process1] process exiting with exitcode 0
[INFO/MainProcess] process shutting down
[DEBUG/MainProcess] running all "atexit" finalizers with priority >= 0
[DEBUG/MainProcess] running the remaining "atexit" finalizers
process1 31980
Processing finished
Is proc1 alive  False
Finished



Previous                                                 Next                                                 Home

No comments:

Post a Comment