获取日志#
备注
自 0.5.2 支持,仅支持分布式环境使用。
对于分布式环境,自定义函数的输出(如通过 mr.remote.spawn
提交),不会在客户端显示。因此,fetch_log
接口可以用来在客户端显示日志。
举个例子,我们定义了一个 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