如何在Ray中使用python登录? [英] How can I use the python logging in Ray?

查看:67
本文介绍了如何在Ray中使用python登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主要功能/流程中使用了日志记录模块,它运行良好,但似乎无法在Actor流程/子流程中运行.如何使其运作?在下面的代码示例中,logging.info在主进程中工作,但在工作进程中失败.谢谢.

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")

推荐答案

有几件事要小心.

  • 首先,您应该在worker内部创建一个新的记录器,因为worker在不同的Python进程上运行.如果您尝试使用在工作程序内部的工作程序外部创建的记录器,则Ray将尝试对记录程序进行酸洗并将其发送到工作器进程,并且在进行酸洗和未酸洗时,Python记录器通常无法正常工作.
  • 第二,您必须确保正确设置了日志记录级别.我使用的是 logger.warning 而不是 logger.info ,因为Python日志记录级别默认设置为警告".
  • 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.

这是一个有效的示例:

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")

这篇关于如何在Ray中使用python登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆