且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Ray中使用python登录?

更新时间:2022-06-17 07:34:53

有几件事要小心.

  • 首先,您应该在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")