Python与sendfile

💡 原文英文,约1200词,阅读约需5分钟。
📝

内容提要

sendfile(2)是UNIX系统调用,提供高效的零拷贝数据传输方式,避免了用户空间的性能损失。Python的socket模块新增了socket.sendfile()方法,利用sendfile(2)实现文件传输,速度通常是socket.send()的两倍。该方法将在Python 3.5中引入,适用于FTP和HTTP服务器。

🎯

关键要点

  • sendfile(2)是UNIX系统调用,提供高效的零拷贝数据传输方式。

  • sendfile(2)在内核中完成数据复制,避免了用户空间的性能损失。

  • 使用sendfile()发送文件的速度通常是使用socket.send()的两倍。

  • Python的socket模块新增了socket.sendfile()方法,适用于FTP和HTTP服务器。

  • socket.sendfile()将在Python 3.5中引入,能够处理socket超时并提供可选参数。

  • sendfile(2)首次出现在Python标准库中是在Python 3.3版本。

  • Windows系统提供类似于sendfile(2)的功能:TransmitFile。

  • 对于使用旧版本Python 2.6和2.7的用户,提供了socket.sendfile()的回退版本。

延伸问答

什么是sendfile(2)系统调用?

sendfile(2)是UNIX系统调用,提供高效的零拷贝数据传输方式,避免了用户空间的性能损失。

Python中如何使用socket.sendfile()方法?

socket.sendfile()方法用于通过socket传输文件,直到文件结束,通常速度是socket.send()的两倍。

sendfile(2)与socket.send()的性能比较如何?

使用sendfile(2)发送文件的速度通常是使用socket.send()的两倍,因为sendfile(2)在内核中完成数据复制。

socket.sendfile()在Python的哪个版本中引入?

socket.sendfile()将在Python 3.5中引入。

Windows系统中是否有类似于sendfile(2)的功能?

是的,Windows系统提供类似于sendfile(2)的功能,称为TransmitFile。

如何在旧版本的Python中使用socket.sendfile()?

对于Python 2.6和2.7用户,可以使用pysendfile模块的回退版本来实现socket.sendfile()的功能。

➡️

继续阅读