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

北战南征 提交于 2019-11-30 14:13:49

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!