Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include worker stdout in log #4502

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions distributed/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import bisect
import errno
import heapq
import io
import logging
import os
import random
Expand All @@ -12,9 +13,9 @@
import weakref
from collections import defaultdict, deque, namedtuple
from collections.abc import MutableMapping
from contextlib import suppress
from contextlib import suppress ExitStack, redirect_stdout
from datetime import timedelta
from functools import partial
from functools import partial, wraps
from inspect import isawaitable
from pickle import PicklingError

Expand Down Expand Up @@ -3721,6 +3722,20 @@ def weight(k, v):
return sizeof(v)


def log_stdout(func):
@wraps(func)
def wrapped(*args, **kwargs):
with ExitStack() as stack:
out, _ = io.StringIO(), io.StringIO()
stack.enter_context(redirect_stdout(out))
try:
return func(*args, **kwargs)
finally:
logger.info(out.getvalue())

return wrapped


async def run(server, comm, function, args=(), kwargs=None, is_coro=None, wait=True):
kwargs = kwargs or {}
function = pickle.loads(function)
Expand All @@ -3743,12 +3758,12 @@ async def run(server, comm, function, args=(), kwargs=None, is_coro=None, wait=T
logger.info("Run out-of-band function %r", funcname(function))
try:
if not is_coro:
result = function(*args, **kwargs)
result = log_stdout(function)(*args, **kwargs)
else:
if wait:
result = await function(*args, **kwargs)
result = await log_stdout(function)(*args, **kwargs)
else:
server.loop.add_callback(function, *args, **kwargs)
server.loop.add_callback(log_stdout(function), *args, **kwargs)
result = None

except Exception as e:
Expand Down