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
setblocking() method on file objects #40229
Comments
Currently file object's write() method returns nothing. Under various situations, such as communicating with Currently, setting a file to unblocking mode requires When you do set a file to non-blocking mode, the file Even using things like select() loops are not reliable, The only sure way to avoid a file object's write() from The existing read() method behaves the same as the |
Implemented in 3.0, 2.x probably won't get this. Will close if nobody |
setblocking() doesn't exist in py3k either, so reopening. I agree it |
Any clue on where to start to do this? |
There are at least two ways to do non-blocking file IO on Windows: Overlapped I/O Don't know what's best here, but happy to see what might be achieved |
Prototype to test nonblocking file objet:
Use test_process.py to test it: this script runs a Python interpreter in a subprocess. It uses select() to check if there is data or not. Eg. type '1+1\n' and then 'exit()\n'. Set PYIO_HAVE_BLOCKING constant to False (in test_process.py) to test the script without io_blocking.patch. I'm not sure that select() is required, but it doesn't work without it (read() blocks). |
I'm not sure that a setblocking() method to fileobjects would make much sense, since non-blocking IO doesn't work with regular files (only pipes, sockets...). |
See the PEP-466 which proposes to add a new os.set_blocking() function on UNIX, and blocking parameter to socket constructor. |
There is a real need of non-blocking operation, but I now think that adding a set_blocking() method is not the right solution. First, as said by Charles-Francois: O_NONBLOCK flag has no effect on regular files. Second, there is no portable way to declare a file as "non blocking": as said by Tim, it is complelty different on Windows (I/O Completion ports). The asyncio module has been merged into Python 3.4: you can now use it to access a file asynchroniously. I don't think that the asyncio is complete for your use case, but if something should done: it is in this module, only in this module, not the API of the io.FileIO class. asyncio can be used to watch pipes of a subprocess asynchroniously. |
That it's not portable is actually a good reason to add a helper function |
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: