检索日志#
注意
版本0.5.2中的新特性,仅适用于分布式调度器。
在分布式环境下,通过 mars.remote.spawn 提交的自定义函数中的日志输出不会在客户端显示,用户可能需要登录到工作节点才能查看日志。因此, fetch_log API 用于将日志检索到客户端以便于显示。
例如,我们定义了一个名为 calculate 的函数。
>>> import mars
>>> # create a distributed session
>>> mars.new_session('http://<ip>:<port>')
>>>
>>> import mars.remote as mr
>>> def calculate(x):
>>> acc = 0
>>> for i in range(x):
>>> acc += i
>>> print(acc)
>>> return acc
然后我们触发执行,可以看到没有任何输出,这正如预期的那样。
>>> r = mr.spawn(calculate, 10)
>>> r.execute()
Object <op=RemoteFunction, key=67b91ee22e4153107c615797bdb6c189>
>>> r.fetch()
45
您可以使用 r.fetch_log() 来检索输出。
>>> print(r.fetch_log())
0
1
3
6
10
15
21
28
36
45
>>> print(r.fetch_log()) # call fetch_log again will continue to fetch
>>> print(r.fetch_log(offsets=0)) # set offsets=0 to retrieve from beginning
0
1
3
6
10
15
21
28
36
45
将 异步执行 与 fetch_log 结合,能够在运行期间持续获取日志。
>>> import time
>>> def c_calc():
>>> for i in range(10):
>>> time.sleep(1)
>>> print(i)
>>>
>>> r = mr.spawn(c_calc)
>>>
>>> def run():
>>> f = r.execute(wait=False)
>>> while not f.done():
>>> time.sleep(0.5)
>>> log = str(r.fetch_log()).strip()
>>> if log:
>>> print(log)
>>>
>>> run()
0
1
2
3
4
5
6
7
8
9