Title: FileInput "inplace" redirects output of other threads
Type: behavior Stage: resolved
Components: IO, Library (Lib) Versions: Python 3.5, Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, mj4int
Priority: normal Keywords:

Created on 2018-02-22 00:01 by mj4int, last changed 2020-10-20 10:14 by iritkatriel. This issue is now closed.

Messages (3)
msg312516 - (view) Author: (mj4int) Date: 2018-02-22 00:01
A pool of threads exists, all of which have started executing.

Thread A has a fileinput object and is currently iterating over the files in "edit in place mode".  For each file, stdout is redirected to the file.  Thread A can call print and write to the file.

Thread B just wants to log some things in the console.  Thread B calls print and... writes to the file thread A is processing.  stdout is hijacked by thread A's fileinput loop.

Whether or not every thread should have an independent evaluation of stdout, certainly a fileinput object shouldn't silently redirect the prints of an innocent bystander thread?

May exist in other python versions, but not checked.
msg378078 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-10-05 23:01
stdout is indeed shared between all threads, and it is also not guaranteed to be thread safe, so if two threads write to it simultaneously that can cause problems.

It is better to have one thread in you program writing to stdout, with thread-safe communication channels from other threads to this one.

Alternatively, you can create different processes instead of threads, and then each process will have its own stdout and redirecting one will not impact the others.
msg378079 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-10-05 23:04
In general, stackoverflow is a better place for questions like this - you are likely to get a quicker response there.
Date User Action Args
2020-10-20 10:14:20iritkatrielsetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-10-05 23:04:17iritkatrielsetmessages: + msg378079
2020-10-05 23:01:11iritkatrielsetnosy: + iritkatriel
messages: + msg378078
2018-02-22 00:01:28mj4intcreate