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

Capturing stdout of child processes in prefect logger #1560

Open
trahloff opened this issue Apr 3, 2024 · 2 comments
Open

Capturing stdout of child processes in prefect logger #1560

trahloff opened this issue Apr 3, 2024 · 2 comments

Comments

@trahloff
Copy link

trahloff commented Apr 3, 2024

Hi joblib team,

It seems like the logger object in joblob results in side-effects like PrefectHQ/prefect#12428.

Do you have any idea why this is the case? I am also more than happy to contribute the fix if you point me to a potential root cause.

@fcharras
Copy link
Contributor

fcharras commented Apr 3, 2024

AFAIK Parallel does not use Logger inheritance, relevant threads #1483 #1494 .

In your example:

from prefect import flow, task
from joblib import Parallel, delayed

def sqrt(x):
    print(x**0.5)


@task
def print_joblib():
    Parallel(n_jobs=2)(delayed(sqrt)(i**2) for i in range(4))


@task
def print_directly():
    print("This is a print statement.")


@flow(log_prints=True)
def main() -> None:
    print_joblib()


if __name__ == "__main__":
    main()

Child processes are just writing to stdout using their respective sys.stdout file handle (see print file argument)

Correct me if I'm wrong, I assume there that prefect captures outputs by wrapping sys.stdout in the processes that are spawned to run tasks, but the wrapper doesn't carry over to the child processes that joblib will in turn spawn from the task process. You most likely will see the same behavior if replacing joblib.Parallel with things like python built-in multiprocessing.Pool or concurrent.futures.ProcessPoolExecutor, etc.

I know of a project that work around this by using file descriptors, see this documented implementation of capturing stdout of child processes using file descriptors, maybe this can help you ?

@fcharras fcharras changed the title Incompatible Use of Logger Object Capturing stdout of child processes in prefect logger Apr 3, 2024
@trahloff
Copy link
Author

trahloff commented Apr 3, 2024

Thank you for the quick reply @fcharras! They basically patch the logger object or patch the print function depending on how they capture the log stream.

@zzstoatzz, does that information help you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants