What File Descriptor object does Python AsyncIO's loop.add_reader() expect?

后端 未结 2 559
攒了一身酷
攒了一身酷 2021-01-02 18:19

I\'m trying to understand how to use the new AsyncIO functionality in Python 3.4 and I\'m struggling with how to use the event_loop.add_reader(). From the limited discussion

相关标签:
2条回答
  • 2021-01-02 19:05

    These functions expect a file descriptor, that is, the underlying integers the operating system uses, not Python's file objects. File objects that are based on file descriptors return that descriptor on the fileno() method, so for example:

    >>> sys.stderr.fileno()
    2
    

    In Unix, file descriptors can be attached to files or a lot of other things, including other processes.

    Edit for the OP's edit:

    As Max in the comments says, you can not use epoll on local files (and asyncio uses epoll). Yes, that's kind of weird. You can use it on pipes, though, for example:

    import asyncio
    import urllib.parse
    import sys
    import pdb
    import os
    
    def fileCallback(*args):
        print("Received: " + sys.stdin.readline())
    
    loop = asyncio.get_event_loop()
    task = loop.add_reader(sys.stdin.fileno(), fileCallback)
    loop.run_forever()
    

    This will echo stuff you write on stdin.

    0 讨论(0)
  • 2021-01-02 19:18

    you cannot use add_reader on local files, because:

    • It cannot be done using select/poll/epoll
    • It depends on the operating system
    • It cannot be fully asynchronous because of os limitations (linux does not support async fs metadata read/write)

    But, technically, yes you should be able to do async filesystem read/write, (almost) all systems have DMA mechanism for doing i/o "in the background". And no, local i/o is not really fast such that no one would want it, the CPU are in the order of millions times faster that disk i/o.

    Look for aiofile or aiofiles if you want to try async i/o

    0 讨论(0)
提交回复
热议问题