homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: `loop.add_reader` and `<
Type: Stage:
Components: asyncio Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, def_bk, vahid.mardani, yselivanov
Priority: normal Keywords:

Created on 2017年03月03日 22:33 by vahid.mardani, last changed 2022年04月11日 14:58 by admin.

Messages (3)
msg288941 - (view) Author: Vahid Mardani (vahid.mardani) Date: 2017年03月03日 22:33
Assume this simple script for reading from stdin:
```python
#! /usr/bin/env python3
import sys
import os
import asyncio
async def main(loop):
 done = False
 fileno = sys.stdin.fileno()
 def _reader():
 nonlocal done
 chunk = os.read(fileno, 1024)
 if not chunk:
 loop.remove_reader(fileno)
 done = True
 return
 print(chunk.decode(), end='')
 loop.add_reader(fileno, _reader)
 while not done:
 await asyncio.sleep(1)
if __name__ == '__main__':
 main_loop = asyncio.get_event_loop()
 main_loop.run_until_complete(main(main_loop))
```
When I run it by:
```bash
$ ./stdin_issue.py <<EOF
> hello
> EOF
```
I get:
```
Traceback (most recent call last):
 File "/usr/lib/python3.5/asyncio/selector_events.py", line 234, in add_reader
 key = self._selector.get_key(fd)
 File "/usr/lib/python3.5/selectors.py", line 191, in get_key
 raise KeyError("{!r} is not registered".format(fileobj)) from None
KeyError: '0 is not registered'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
 File "./stdin_issue.py", line 41, in <module>
 main_loop.run_until_complete(main(main_loop))
 File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
 return future.result()
 File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
 raise self._exception
 File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
 result = coro.send(None)
 File "./stdin_issue.py", line 34, in main
 loop.add_reader(fileno, _reader)
 File "/usr/lib/python3.5/asyncio/selector_events.py", line 237, in add_reader
 (handle, None))
 File "/usr/lib/python3.5/selectors.py", line 411, in register
 self._epoll.register(key.fd, epoll_events)
PermissionError: [Errno 1] Operation not permitted
```
But the:
```bash
echo "Hello" | ./stdin_issue.py 
```
 Is working well.
I was already tried this with the `select.select` directly, and it working:
```
def using_select():
 files = [sys.stdin.fileno()]
 while True:
 readables, _, errors = select(files, [], files)
 if errors:
 print('ERROR:', errors)
 return
 if readables:
 for f in readables:
 chunk = os.read(f, 1024)
 if not chunk:
 return
 print(chunk.decode(), end='')
```
msg345110 - (view) Author: Alexey Kostyrin (def_bk) Date: 2019年06月10日 09:00
Checked latest master (c879ff247ae1b67a790ff98d2d59145302cd4e4e), 3.8 - 3.5 branches - can not reproduce.
OS - macOS
msg345112 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2019年06月10日 09:42
If fd is a regular file it is not supported well by all select/poll/epoll.
Registration may succeed but the data is marked as "already ready for reading".
Not sure if we should do anything.
The situation may be changed after eventual providing asyncio API for files (there is no actual work for it yet).
History
Date User Action Args
2022年04月11日 14:58:43adminsetgithub: 73903
2019年06月10日 09:42:11asvetlovsetmessages: + msg345112
2019年06月10日 09:00:04def_bksetnosy: + def_bk
messages: + msg345110
2019年03月19日 12:11:18cheryl.sabellasetnosy: + asvetlov, - gvanrossum
versions: + Python 3.8, - Python 3.5
2017年03月03日 22:33:57vahid.mardanicreate

AltStyle によって変換されたページ (->オリジナル) /