-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
multiprocessing.process using os.close(sys.stdin.fileno) instead of sys.stdin.close() #49563
Comments
In multiprocessing.process it contains the code: def _bootstrap(self):
....
if sys.stdin is not None:
try:
os.close(sys.stdin.fileno())
except (OSError, ValueError):
pass This code should probably be calling sys.stdin.close() and not The code as is will fail if sys.stdin had been replaced with an alternate |
Joshua Judson Rosen to python-list Jesse Noller <jnoller@gmail.com> writes:
My guess would be: because it's also possible for sys.stdin to be a There's a general guideline, inherited from C, that one should ensure So, if you call sys.stdin.close() in the child-process in You can expect similar issues with just about /any/ As such, I'd recommend against just using .close(); you might use I guess you could try calling that an |
Hello, I believe I am the edge-case. I've written a minimalist python to reproduce run papy_gui.py
and type:
>>> from math import sqrt
>>> from multiprocessing import Pool
>>> p = Pool()
>>> print p
<multiprocessing.pool.Pool object at 0xb723738c>
>>> p.map(sqrt, [1,2,3])
<<no more output ever>> |
Using sys.stdin.close() fixes issues 5155 and 5331. |
Worth noting is that Python documentation in: http://docs.python.org/library/stdtypes.html says: """ Note File-like objects which do not have a real file descriptor should So, if sys.stdin is replaced with a file like object, then the code At the moment the code doesn't check for AttributeError which is what is """
>>> import StringIO
>>> s=StringIO.StringIO("xxx")
>>> s.fileno()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: StringIO instance has no attribute 'fileno'
""" Thus error propagates. The better check though would be to use: def _bootstrap(self):
....
if sys.stdin is not None and hasattr(sys.stdin, "fileno"):
try:
os.close(sys.stdin.fileno())
except (OSError, ValueError):
pass That is, only actually call fileno() if it is present. This would solve the problem for the original reported issue by making This change wouldn't necessarily solve other peoples related issues |
Wouldn't it be more pythonic to just try sys.stdin.fileno() and catch def _bootstrap(self):
....
try:
os.close(sys.stdin.fileno())
except AtributeError:
sys.stdin.close()
except (OSError, ValueError):
pass |
Closing the stdin fd without closing the stdin file is very dangerous. Closing stdin is useful to let the parent be alone in reading from it. The "double flush" case is impossible to handle in general. It's the So that code in _bootstrap should be: |
Attached is a patch which calls close() first, and then attempts to close This is just a thought, feedback welcome - right now this allows this |
Please do this: --- a/multiprocessing/process.py
+++ b/multiprocessing/process.py
@@ -225,7 +225,8 @@ class Process(object):
self._children = set()
self._counter = itertools.count(1)
try:
- os.close(sys.stdin.fileno())
+ sys.stdin.close()
+ sys.stdin = open(os.devnull)
except (OSError, ValueError):
pass
_current_process = self One shouldn't close the fileno after the file has been closed. The The open(os.devnull) bit is both so that no one tries to do anything |
On Fri, Jun 19, 2009 at 11:55 AM, OG7<report@bugs.python.org> wrote:
Fair enough, I was worried a bit about skipping the |
Attached is a patch with docs, tests and the fix as suggested by OG7 (whom |
Committed in r73708 on trunk |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: