# Support for os.sendfile for Windows

**URL:** https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020
**Category:** Ideas
**Created:** [March 20, 2023, 8:01am UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020 "2023-03-20T08:01:07Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![KhazAkar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/khazakar/32/11640_2.png) [@KhazAkar](https://discuss.python.org/u/KhazAkar)
#### Post date: [March 20, 2023, 8:01am UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/1 "2023-03-20T08:01:08Z")

</div>

Currently, os module for windows does not have support for sendfile. However, it exists for Windows family of operating systems, but only for sockets.

I think it would be great to close one of the gaps between POSIX operating systems and Windows, at least for that. Here’s windows socket call called TransferFile, which should work similarly as os.sendfile is.  
[TransmitFile at learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/mswsock/nf-mswsock-transmitfile)

---

<div class="post-metadata">

### Author: ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)
#### Post date: [March 20, 2023, 9:08am UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/2 "2023-03-20T09:08:47Z")

</div>

Maybe open an issue on the [CPython issue tracker](https://github.com/python/cpython/issues/)?

---

<div class="post-metadata">

### Author: ![KhazAkar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/khazakar/32/11640_2.png) [@KhazAkar](https://discuss.python.org/u/KhazAkar)
#### Post date: [March 20, 2023, 9:21am UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/3 "2023-03-20T09:21:49Z")

</div>

If you see this valuable, then sure. When doing the issue on GitHub, it deliberately asks if this was first discussed in ideas/discuss page outside GitHub, so I did that 🙂

---

<div class="post-metadata">

### Author: ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)
#### Post date: [March 20, 2023, 10:59am UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/4 "2023-03-20T10:59:06Z")

</div>

Fair enough—just to be clear, I have no particular opinion, nor am I particularly qualified on this topic. It just seemed to me that adding missing support for a Tier 1 platform to an existing standard library function (as opposed to, e.g., introducing a whole new function) seemed far enough from a brand new feature driven by conceptual and usability concerns, and close enough to a detail enhancement/defect fix driven by technical and implementation ones, to be a better fit for the issue tracker. However, it’s perfectly possible I’m misjudging this.

---

<div class="post-metadata">

### Author: ![KhazAkar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/khazakar/32/11640_2.png) [@KhazAkar](https://discuss.python.org/u/KhazAkar)
#### Post date: [March 20, 2023, 11:52am UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/5 "2023-03-20T11:52:42Z")

</div>

Then I will create issue on GitHub as soon as possible 🙂  
It’s not the only one missing, other things like usage of WSAAsyncSelect for select.select function etc.

---

<div class="post-metadata">

### Author: ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)
#### Post date: [March 20, 2023, 12:54pm UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/6 "2023-03-20T12:54:40Z")

</div>

IMO, I’m all for reducing the delta between what’s implemented on POSIX platforms vs. on Windows (so long as there aren’t subtle but significant differences we can’t account for that could bite unwary users), so great to see you’re looking in to that. Thanks!

---

<div class="post-metadata">

### Author: ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)
#### Post date: [March 20, 2023, 2:56pm UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/7 "2023-03-20T14:56:36Z")

</div>

`TransmitFile()` is supported by the asyncio proactor event loop `sendfile()` and `sock_sendfile()` methods. Here’s a snippet of the low-level implementation in “Modules/overlapped.c”:

> <https://github.com/python/cpython/blob/5e6661bce968173fa45b74fa2111098645ff609c/Modules/overlapped.c#L1497-L1547>

* * *

> [@KhazAkar](#):
>
> other things like usage of WSAAsyncSelect for select.select function etc.

[`WSAAsyncSelect()`](https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsaasyncselect) isn’t applicable to implementing `select.select()`, and it isn’t a good fit for the standard library. It integrates socket I/O into a GUI window message loop. That’s for a GUI toolkit to handle, such as Qt for example:

> <https://github.com/qt/qtbase/blob/aa37e67ef7f5ff22da0ef95fb5221bc1fff9b3ca/src/corelib/kernel/qeventdispatcher_win.cpp#L405-L411>

OTOH, supporting [`WSAEventSelect()`](https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaeventselect) and [`WSAEnumNetworkEvents()`](https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaenumnetworkevents) would be useful for integrating socket I/O with `WaitForMultipleObjects()` waits.

---

<div class="post-metadata">

### Author: ![KhazAkar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/khazakar/32/11640_2.png) [@KhazAkar](https://discuss.python.org/u/KhazAkar)
#### Post date: [March 20, 2023, 3:00pm UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/8 "2023-03-20T15:00:44Z")

</div>

Thank you a lot for this insight! I didn’t know that WSAAsyncSelect is for GUI window message loop and that TransmitFile is already in!  
Starting from which Python version it’s usable for everybody? And additionally - can we use it without asyncio? 🙂

Later on I will take a look at other things, like more socket related ioctl methods available on Windows, but not exposed to python 🙂  
Thanks again!

---

<div class="post-metadata">

### Author: ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)
#### Post date: [March 20, 2023, 3:03pm UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/9 "2023-03-20T15:03:39Z")

</div>

> [@CAM-Gerlach](#):
>
> (so long as there aren’t subtle but significant differences we can’t account for that could bite unwary users),

Seen in a vacuum, “only works on sockets” is a pretty significant and annoying difference. But that exact same distinction also applies to quite a few other Windows features (where winsock is its own thing, in contrast to Unix where those features are available on all FDs), making it less confusing.

+1. Definitely worth adding.

---

<div class="post-metadata">

### Author: ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)
#### Post date: [March 20, 2023, 3:56pm UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/10 "2023-03-20T15:56:58Z")

</div>

> [@KhazAkar](#):
>
> Starting from which Python version it’s usable for everybody? And additionally - can we use it without asyncio?

The `Overlapped.TransmitFile()` method is implemented in Python 3.7+ in the `_overlapped` extension module. It’s undocumented and only intended for internal use in the standard library. That said, it’s technically possible to use it outside of the `asyncio` module, but it’s awkward. For example:

```auto
>>> import socket, msvcrt, _overlapped
>>> f = open('spam.txt', 'wb+')
>>> f.write(b'eggs')
4
>>> f.flush(); f.seek(0)
0
>>> h = msvcrt.get_osfhandle(f.fileno())
>>> sr, sw = socket.socketpair()
>>> ov = _overlapped.Overlapped()
>>> ov.TransmitFile(sw.fileno(), h, 0, 0, 4, 4, 0)
>>> ov.getresult(True)
4
>>> sr.recv(4)
b'eggs'

```

---

<div class="post-metadata">

### Author: ![KhazAkar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/khazakar/32/11640_2.png) [@KhazAkar](https://discuss.python.org/u/KhazAkar)
#### Post date: [March 20, 2023, 4:05pm UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/11 "2023-03-20T16:05:57Z")

</div>

Thanks a lot! I have a question then - if most of the work was done to support this, why it’s not hooked onto os.sendfile? That would make life much easier in terms of code portability etc.  
If there’s issue with time needed for somebody to implement it, maybe I might jump in (if it does not involve C too much, my skills are not great 😃 ) and implement that for os.sendfile in my rather limited, but still available, spare time 🙂

---

<div class="post-metadata">

### Author: ![Archmonger](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/archmonger/32/18233_2.png) [@Archmonger](https://discuss.python.org/u/Archmonger)
#### Post date: [September 28, 2024, 12:43am UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/12 "2024-09-28T00:43:33Z")

</div>

Agreed that it seems fairly strange that all the plumbing has been available since Python 3.7, but it is not publicly exposed on `win32` via `os.sendfile`.

Is there a historical reason behind this decision?

---

<div class="post-metadata">

### Author: ![KhazAkar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/khazakar/32/11640_2.png) [@KhazAkar](https://discuss.python.org/u/KhazAkar)
#### Post date: [September 28, 2024, 6:02am UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/13 "2024-09-28T06:02:12Z")

</div>

I don’t know much of a details, but I know that it was discussed a long time ago here: [https://bugs.python.org/issue21721](https://bugs.python.org/issue21721)

---

<div class="post-metadata">

### Author: ![Archmonger](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/archmonger/32/18233_2.png) [@Archmonger](https://discuss.python.org/u/Archmonger)
#### Post date: [September 29, 2024, 2:10am UTC](https://discuss.python.org/t/support-for-os-sendfile-for-windows/25020/14 "2024-09-29T02:10:50Z")

</div>

Looks like there is an open PR for this feature

> <https://github.com/python/cpython/pull/112337/files>
>
> \- Using the \`Overlapped.TransmitFile\` in \`\_overlapped\` module to implement \`sock…et.sendfile\`;
> \- Add \`Overlapped.getresultex\` to implement the timeout functionality, which have almost the same codes as exists \`Overlapped.getresult\`, just with diffenernt arguments. I split the \`Overlapped.getresult\` in to another function to share the codes.
> 
> And some thought on \`os.sendfile\`: https://github.com/python/cpython/issues/102898#issuecomment-1824554063
> 
> \<!--
> Thanks for your contribution!
> Please read this comment in its entirety. It's quite important.
> 
> \# Pull Request title
> 
> It should be in the following format:
> 
> \`\`\`
> gh-NNNNN: Summary of the changes made
> \`\`\`
> 
> Where: gh-NNNNN refers to the GitHub issue number.
> 
> Most PRs will require an issue number. Trivial changes, like fixing a typo, do not need an issue.
> 
> \# Backport Pull Request title
> 
> If this is a backport PR (PR made against branches other than \`main\`),
> please ensure that the PR title is in the following format:
> 
> \`\`\`
> \[X.Y\] \<title from the original PR\> (GH-NNNN)
> \`\`\`
> 
> Where: \[X.Y\] is the branch name, e.g. \[3.6\].
> 
> GH-NNNN refers to the PR number from \`main\`.
> 
> \--\>
> 
> 
> 
> \* Issue: gh-65920
