How can I use the python logging in Ray?

♀尐吖头ヾ 提交于 2021-02-07 13:19:33

问题


I use the logging module in the main function/process, it works well, but it seems can't work in Actor process/subprocess. How to make it work? In the sample below code, logging.info work in the main process but failed in the worker process. Thanks.

import logging
import ray

@ray.remote
class Worker(object):
   ...

   def train(self):
       logging.info("fail print")


...

worker = Worker.remote()

ray.get(worker.train.remote())

logging.info("successful print")


回答1:


There are a couple things to be careful about.

  • First, you should create a new logger inside of the worker because the worker runs on a different Python process. If you try to use a logger that you created outside of the worker within the worker, then Ray will try to pickle the logger and send it to the worker process, and Python loggers typically do not behave correctly when pickled and unpickled.
  • Second, you have to make sure the logging level is set correctly. I'm using logger.warning instead of logger.info because the Python logging level is set to `warning by default.

Here is a working example:

import logging
import ray

logger = logging.getLogger(__name__)

@ray.remote
class Worker(object):
    def __init__(self):
        self.logger = logging.getLogger(__name__)
    def train(self):
        self.logger.warning("print from inside worker")


ray.init()

worker = Worker.remote()

ray.get(worker.train.remote())

logger.warning("print from outside worker")



回答2:


I've had trouble with Ray suppressing console output from loggers. To get around that, add a StreamHandler:

your_logger.addHandler(logging.StreamHandler())


来源:https://stackoverflow.com/questions/55272066/how-can-i-use-the-python-logging-in-ray

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!