Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c9c2c8b0
Commit
c9c2c8b0
authored
Feb 25, 2011
by
Giampaolo Rodolà
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 10882: add os.sendfile(). (patch provided by Ross Lagerwall)
parent
59db1f3d
Changes
7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
550 additions
and
21 deletions
+550
-21
Doc/library/os.rst
Doc/library/os.rst
+41
-0
Doc/whatsnew/3.3.rst
Doc/whatsnew/3.3.rst
+13
-0
Lib/test/test_os.py
Lib/test/test_os.py
+253
-0
Misc/NEWS
Misc/NEWS
+2
-0
Modules/posixmodule.c
Modules/posixmodule.c
+232
-16
configure.in
configure.in
+6
-5
pyconfig.h.in
pyconfig.h.in
+3
-0
No files found.
Doc/library/os.rst
View file @
c9c2c8b0
...
...
@@ -775,6 +775,47 @@ as internal buffering of data.
:meth:`~file.readline` methods.
.. function:: sendfile(out, in, offset, nbytes)
sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)
Copy *nbytes* bytes from file descriptor *in* to file descriptor *out*
starting at *offset*.
Return the number of bytes sent. When EOF is reached return 0.
The first function notation is supported by all platforms that define
:func:`sendfile`.
On Linux, if *offset* is given as ``None``, the bytes are read from the
current position of *in* and the position of *in* is updated.
The second case may be used on Mac OS X and FreeBSD where *headers* and
*trailers* are arbitrary sequences of buffers that are written before and
after the data from *in* is written. It returns the same as the first case.
On Mac OS X and FreeBSD, a value of 0 for *nbytes* specifies to send until
the end of *in* is reached.
On Solaris, *out* may be the file descriptor of a regular file or the file
descriptor of a socket. On all other platforms, *out* must be the file
descriptor of an open socket.
Availability: Unix.
.. versionadded:: 3.3
.. data:: SF_NODISKIO
SF_MNOWAIT
SF_SYNC
Parameters to the :func:`sendfile` function, if the implementation supports
them.
Availability: Unix.
.. versionadded:: 3.3
.. function:: tcgetpgrp(fd)
Return the process group associated with the terminal given by *fd* (an open
...
...
Doc/whatsnew/3.3.rst
View file @
c9c2c8b0
...
...
@@ -68,6 +68,19 @@ New, Improved, and Deprecated Modules
* Stub
os
--
The :mod:`os` module has a new :func:`~os.sendfile` function which provides an
efficent "zero-copy" way for copying data from one file (or socket) descriptor
to another.
The phrase "zero-copy" refers to the fact that all of the copying of data
between the two descriptors is done entirely by the kernel, with no copying of
data into userspace buffers.
:func:`~os.sendfile` can be used to efficiently copy data from a file on disk to
a network socket, e.g. for downloading a file.
(Patch submitted by Ross Lagerwall and Giampaolo Rodolà in :issue:`10882`.)
Optimizations
=============
...
...
Lib/test/test_os.py
View file @
c9c2c8b0
...
...
@@ -15,6 +15,13 @@ from test import support
import
contextlib
import
mmap
import
uuid
import
asyncore
import
asynchat
import
socket
try
:
import
threading
except
ImportError
:
threading
=
None
# Detect whether we're on a Linux system that uses the (now outdated
# and unmaintained) linuxthreads threading library. There's an issue
...
...
@@ -1261,6 +1268,251 @@ class LoginTests(unittest.TestCase):
self
.
assertNotEqual
(
len
(
user_name
),
0
)
class
SendfileTestServer
(
asyncore
.
dispatcher
,
threading
.
Thread
):
class
Handler
(
asynchat
.
async_chat
):
def
__init__
(
self
,
conn
):
asynchat
.
async_chat
.
__init__
(
self
,
conn
)
self
.
in_buffer
=
[]
self
.
closed
=
False
self
.
push
(
b"220 ready
\
r
\
n
"
)
def
handle_read
(
self
):
data
=
self
.
recv
(
4096
)
self
.
in_buffer
.
append
(
data
)
def
get_data
(
self
):
return
b''
.
join
(
self
.
in_buffer
)
def
handle_close
(
self
):
self
.
close
()
self
.
closed
=
True
def
handle_error
(
self
):
raise
def
__init__
(
self
,
address
):
threading
.
Thread
.
__init__
(
self
)
asyncore
.
dispatcher
.
__init__
(
self
)
self
.
create_socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
bind
(
address
)
self
.
listen
(
5
)
self
.
host
,
self
.
port
=
self
.
socket
.
getsockname
()[:
2
]
self
.
handler_instance
=
None
self
.
_active
=
False
self
.
_active_lock
=
threading
.
Lock
()
# --- public API
@
property
def
running
(
self
):
return
self
.
_active
def
start
(
self
):
assert
not
self
.
running
self
.
__flag
=
threading
.
Event
()
threading
.
Thread
.
start
(
self
)
self
.
__flag
.
wait
()
def
stop
(
self
):
assert
self
.
running
self
.
_active
=
False
self
.
join
()
def
wait
(
self
):
# wait for handler connection to be closed, then stop the server
while
not
getattr
(
self
.
handler_instance
,
"closed"
,
True
):
time
.
sleep
(
0.001
)
self
.
stop
()
# --- internals
def
run
(
self
):
self
.
_active
=
True
self
.
__flag
.
set
()
while
self
.
_active
and
asyncore
.
socket_map
:
self
.
_active_lock
.
acquire
()
asyncore
.
loop
(
timeout
=
0.001
,
count
=
1
)
self
.
_active_lock
.
release
()
asyncore
.
close_all
()
def
handle_accept
(
self
):
conn
,
addr
=
self
.
accept
()
self
.
handler_instance
=
self
.
Handler
(
conn
)
def
handle_connect
(
self
):
self
.
close
()
handle_read
=
handle_connect
def
writable
(
self
):
return
0
def
handle_error
(
self
):
raise
@
unittest
.
skipUnless
(
hasattr
(
os
,
'sendfile'
),
"test needs os.sendfile()"
)
class
TestSendfile
(
unittest
.
TestCase
):
DATA
=
b"12345abcde"
*
1024
*
1024
# 10 Mb
SUPPORT_HEADERS_TRAILERS
=
not
sys
.
platform
.
startswith
(
"linux"
)
and
\
not
sys
.
platform
.
startswith
(
"solaris"
)
@
classmethod
def
setUpClass
(
cls
):
with
open
(
support
.
TESTFN
,
"wb"
)
as
f
:
f
.
write
(
cls
.
DATA
)
@
classmethod
def
tearDownClass
(
cls
):
support
.
unlink
(
support
.
TESTFN
)
def
setUp
(
self
):
self
.
server
=
SendfileTestServer
((
support
.
HOST
,
0
))
self
.
server
.
start
()
self
.
client
=
socket
.
socket
()
self
.
client
.
connect
((
self
.
server
.
host
,
self
.
server
.
port
))
self
.
client
.
settimeout
(
1
)
# synchronize by waiting for "220 ready" response
self
.
client
.
recv
(
1024
)
self
.
sockno
=
self
.
client
.
fileno
()
self
.
file
=
open
(
support
.
TESTFN
,
'rb'
)
self
.
fileno
=
self
.
file
.
fileno
()
def
tearDown
(
self
):
self
.
file
.
close
()
self
.
client
.
close
()
if
self
.
server
.
running
:
self
.
server
.
stop
()
def
sendfile_wrapper
(
self
,
sock
,
file
,
offset
,
nbytes
,
headers
=
[],
trailers
=
[]):
"""A higher level wrapper representing how an application is
supposed to use sendfile().
"""
while
1
:
try
:
if
self
.
SUPPORT_HEADERS_TRAILERS
:
return
os
.
sendfile
(
sock
,
file
,
offset
,
nbytes
,
headers
,
trailers
)
else
:
return
os
.
sendfile
(
sock
,
file
,
offset
,
nbytes
)
except
OSError
as
err
:
if
err
.
errno
==
errno
.
ECONNRESET
:
# disconnected
raise
elif
err
.
errno
in
(
errno
.
EAGAIN
,
errno
.
EBUSY
):
# we have to retry send data
continue
else
:
raise
def
test_send_whole_file
(
self
):
# normal send
total_sent
=
0
offset
=
0
nbytes
=
4096
while
1
:
sent
=
self
.
sendfile_wrapper
(
self
.
sockno
,
self
.
fileno
,
offset
,
nbytes
)
if
sent
==
0
:
break
offset
+=
sent
total_sent
+=
sent
self
.
assertTrue
(
sent
<=
nbytes
)
self
.
assertEqual
(
offset
,
total_sent
)
self
.
assertEqual
(
total_sent
,
len
(
self
.
DATA
))
self
.
client
.
close
()
self
.
server
.
wait
()
data
=
self
.
server
.
handler_instance
.
get_data
()
self
.
assertEqual
(
hash
(
data
),
hash
(
self
.
DATA
))
def
test_send_at_certain_offset
(
self
):
# start sending a file at a certain offset
total_sent
=
0
offset
=
len
(
self
.
DATA
)
/
2
nbytes
=
4096
while
1
:
sent
=
self
.
sendfile_wrapper
(
self
.
sockno
,
self
.
fileno
,
offset
,
nbytes
)
if
sent
==
0
:
break
offset
+=
sent
total_sent
+=
sent
self
.
assertTrue
(
sent
<=
nbytes
)
self
.
client
.
close
()
self
.
server
.
wait
()
data
=
self
.
server
.
handler_instance
.
get_data
()
expected
=
self
.
DATA
[
int
(
len
(
self
.
DATA
)
/
2
):]
self
.
assertEqual
(
total_sent
,
len
(
expected
))
self
.
assertEqual
(
hash
(
data
),
hash
(
expected
))
def
test_offset_overflow
(
self
):
# specify an offset > file size
offset
=
len
(
self
.
DATA
)
+
4096
sent
=
os
.
sendfile
(
self
.
sockno
,
self
.
fileno
,
offset
,
4096
)
self
.
assertEqual
(
sent
,
0
)
self
.
client
.
close
()
self
.
server
.
wait
()
data
=
self
.
server
.
handler_instance
.
get_data
()
self
.
assertEqual
(
data
,
b''
)
def
test_invalid_offset
(
self
):
with
self
.
assertRaises
(
OSError
)
as
cm
:
os
.
sendfile
(
self
.
sockno
,
self
.
fileno
,
-
1
,
4096
)
self
.
assertEqual
(
cm
.
exception
.
errno
,
errno
.
EINVAL
)
# --- headers / trailers tests
if
SUPPORT_HEADERS_TRAILERS
:
def
test_headers
(
self
):
total_sent
=
0
sent
=
os
.
sendfile
(
self
.
sockno
,
self
.
fileno
,
0
,
4096
,
headers
=
[
b"x"
*
512
])
total_sent
+=
sent
offset
=
4096
nbytes
=
4096
while
1
:
sent
=
self
.
sendfile_wrapper
(
self
.
sockno
,
self
.
fileno
,
offset
,
nbytes
)
if
sent
==
0
:
break
total_sent
+=
sent
offset
+=
sent
expected_data
=
b"x"
*
512
+
self
.
DATA
self
.
assertEqual
(
total_sent
,
len
(
expected_data
))
self
.
client
.
close
()
self
.
server
.
wait
()
data
=
self
.
server
.
handler_instance
.
get_data
()
self
.
assertEqual
(
hash
(
data
),
hash
(
expected_data
))
def
test_trailers
(
self
):
TESTFN2
=
support
.
TESTFN
+
"2"
f
=
open
(
TESTFN2
,
'wb'
)
f
.
write
(
b"abcde"
)
f
.
close
()
f
=
open
(
TESTFN2
,
'rb'
)
try
:
os
.
sendfile
(
self
.
sockno
,
f
.
fileno
(),
0
,
4096
,
trailers
=
[
b"12345"
])
self
.
client
.
close
()
self
.
server
.
wait
()
data
=
self
.
server
.
handler_instance
.
get_data
()
self
.
assertEqual
(
data
,
b"abcde12345"
)
finally
:
os
.
remove
(
TESTFN2
)
if
hasattr
(
os
,
"SF_NODISKIO"
):
def
test_flags
(
self
):
try
:
os
.
sendfile
(
self
.
sockno
,
self
.
fileno
,
0
,
4096
,
flags
=
os
.
SF_NODISKIO
)
except
OSError
as
err
:
if
err
.
errno
not
in
(
errno
.
EBUSY
,
errno
.
EAGAIN
):
raise
def
test_main
():
support
.
run_unittest
(
FileTests
,
...
...
@@ -1281,6 +1533,7 @@ def test_main():
PidTests
,
LoginTests
,
LinkTests
,
TestSendfile
,
)
if
__name__
==
"__main__"
:
...
...
Misc/NEWS
View file @
c9c2c8b0
...
...
@@ -35,6 +35,8 @@ Core and Builtins
Library
-------
- Issue 10882: Add os.sendfile function.
- Issue #10868: Allow usage of the register method of an ABC as a class
decorator.
...
...
Modules/posixmodule.c
View file @
c9c2c8b0
This diff is collapsed.
Click to expand it.
configure.in
View file @
c9c2c8b0
...
...
@@ -1283,10 +1283,10 @@ shadow.h signal.h stdint.h stropts.h termios.h thread.h \
unistd.h utime.h \
sys/audioio.h sys/bsdtty.h sys/epoll.h sys/event.h sys/file.h sys/loadavg.h \
sys/lock.h sys/mkdev.h sys/modem.h \
sys/param.h sys/poll.h sys/select.h sys/s
ocket.h sys/statvfs.h sys/stat
.h \
sys/termio.h sys/time.h \
sys/times.h sys/types.h sys/u
n.h sys/utsname.h sys/wait.h pty.h libutil
.h \
sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
sys/param.h sys/poll.h sys/select.h sys/s
endfile.h sys/socket.h sys/statvfs
.h \
sys/
stat.h sys/
termio.h sys/time.h \
sys/times.h sys/types.h sys/u
io.h sys/un.h sys/utsname.h sys/wait.h pty
.h \
libutil.h
sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
bluetooth/bluetooth.h linux/tipc.h spawn.h util.h)
AC_HEADER_DIRENT
AC_HEADER_MAJOR
...
...
@@ -1891,6 +1891,7 @@ AC_MSG_RESULT($SHLIBS)
# checks for libraries
AC_CHECK_LIB(sendfile, sendfile)
AC_CHECK_LIB(dl, dlopen) # Dynamic linking for SunOS/Solaris and SYSV
AC_CHECK_LIB(dld, shl_load) # Dynamic linking for HP-UX
...
...
@@ -2539,7 +2540,7 @@ AC_CHECK_FUNCS(alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
initgroups kill killpg lchmod lchown lstat mbrtowc mkfifo mknod mktime \
mremap nice pathconf pause plock poll pthread_init \
putenv readlink realpath \
select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \
select sem_open sem_timedwait sem_getvalue sem_unlink se
ndfile se
tegid seteuid \
setgid \
setlocale setregid setreuid setresuid setresgid setsid setpgid setpgrp setuid setvbuf \
sigaction siginterrupt sigrelse snprintf strftime strlcpy \
...
...
pyconfig.h.in
View file @
c9c2c8b0
...
...
@@ -1024,6 +1024,9 @@
/* The size of `size_t', as computed by sizeof. */
#undef SIZEOF_SIZE_T
/* Define to 1 if you have the `sendfile' function. */
#undef HAVE_SENDFILE
/* The size of `time_t', as computed by sizeof. */
#undef SIZEOF_TIME_T
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment