Skip to content

Add redirect_stdout(..., per_thread: bool = False)#167

Open
Liam-DeVoe wants to merge 3 commits intoQuansight-Labs:mainfrom
Liam-DeVoe:redirect-stdout
Open

Add redirect_stdout(..., per_thread: bool = False)#167
Liam-DeVoe wants to merge 3 commits intoQuansight-Labs:mainfrom
Liam-DeVoe:redirect-stdout

Conversation

@Liam-DeVoe
Copy link
Copy Markdown
Contributor

Closes #29.

This design takes inspiration from the design sketched by @colesbury in python/cpython#130148 (comment), with some changes required to support interleaving per_thread=False and per_thread=True.

If a per_thread=False context manager is entered (from any thread) while a per_thread=True context manager is active in some thread A, output from A will go to the per_thread=False stream. IMO this matches the expectation that redirect_stdout captures all output from all threads; if you don't want this behavior, pass per_thread=True.

This test illustrates the new thread–isolation capabilities:

n_threads = 10
barrier1 = Barrier(n_threads)
barrier2 = Barrier(n_threads)
bufs = [StringIO() for _ in range(n_threads)]

def f(n, stream):
    barrier1.wait()
    with redirect_stdout(stream, per_thread=True):
        print(f"thread {n}")
        barrier2.wait()

threads = [Thread(target=f, args=(i, bufs[i])) for i in range(n_threads)]

for t in threads:
    t.start()
for t in threads:
    t.join()

for i in range(n_threads):
    assert bufs[i].getvalue() == f"thread {i}\n"

Comment thread .gitignore
Comment on lines +1 to +2
.claude/settings.local.json

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I used claude to iterate on this design, but I stand by every line in this PR myself!)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's cool, but please don't add personal files to project .gitignores. That's what .git/info/exclude is for.

  • Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow) should go into the $GIT_DIR/info/exclude file.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(To be clear, when I say "that's cool", it's a completely meaningless phrase because I'm not a committer here, and I have no idea what the project policy is on AI contributions. It may be cool, or it may be very not cool.) ¯\_(ツ)_/¯

Copy link
Copy Markdown
Contributor Author

@Liam-DeVoe Liam-DeVoe Jan 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree in principle, in practice, if one expects a tool to be used by multiple developers, it's pragmatic to add that tool's ignorable files to the project's .gitignore.

For example, projects generally gitignore editor config files like .vscode/, even though the project is not related to any specific editor.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this is perfectly fine to add

@Liam-DeVoe
Copy link
Copy Markdown
Contributor Author

Liam-DeVoe commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really amazing work @Liam-DeVoe! Great set of tests as well!

@ngoldbaum
Copy link
Copy Markdown
Collaborator

@Liam-DeVoe I'm planning to go over this with you a little bit during our meeting later today.

@Liam-DeVoe
Copy link
Copy Markdown
Contributor Author

Yup sounds like a great use of time 👍

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

Successfully merging this pull request may close these issues.

Provide a facility for thread-safe stdout/stderr redirection

5 participants