Skip to content

loop.add_reader and <<EOF #73903

Closed as not planned
Closed as not planned

Description

@pylover
mannequin
BPO 29717
Nosy @asvetlov, @1st1, @pylover

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:

assignee = None
closed_at = None
created_at = <Date 2017-03-03.22:33:57.914>
labels = ['3.8', 'expert-asyncio']
title = '`loop.add_reader` and `<<EOF`'
updated_at = <Date 2019-06-10.09:42:11.236>
user = 'https://github.com/pylover'

bugs.python.org fields:

activity = <Date 2019-06-10.09:42:11.236>
actor = 'asvetlov'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['asyncio']
creation = <Date 2017-03-03.22:33:57.914>
creator = 'vahid.mardani'
dependencies = []
files = []
hgrepos = []
issue_num = 29717
keywords = []
message_count = 3.0
messages = ['288941', '345110', '345112']
nosy_count = 4.0
nosy_names = ['asvetlov', 'yselivanov', 'vahid.mardani', 'def_bk']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue29717'
versions = ['Python 3.8']

Activity

  1. pylover commented on Mar 3, 2017

    pylovermannequin
    MannequinAuthor

    Assume this simple script for reading from stdin:

    #! /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:

    $ ./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:

    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='')
    
  2. defbk commented on Jun 10, 2019

    defbkmannequin
    Mannequin

    Checked latest master (c879ff2), 3.8 - 3.5 branches - can not reproduce.
    OS - macOS

  3. asvetlov commented on Jun 10, 2019

    @asvetlov
    Contributor

    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).

  4. transferred this issue fromon Apr 10, 2022
  5. kumaraditya303 commented on May 20, 2022

    @kumaraditya303
    Contributor

    As Andrew explained, there isn't something to be fixed here as using a blocking fd is not supported hence closing.

  6. allisonkarlitskaya commented on Oct 30, 2022

    @allisonkarlitskaya
    Contributor

    Also happens for /dev/null.

  7. gvanrossum commented on Oct 30, 2022

    @gvanrossum
    Member

    Yeah, /dev/null is more like a regular file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions