Skip to content

Commit 3fffd06

Browse files
committed
streams: Fix _try_write method; add more debug stats
1 parent 4ecf55d commit 3fffd06

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

uvloop/handles/stream.pyx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,11 @@ cdef class UVStream(UVBaseTransport):
158158
self.__reading_stopped()
159159

160160
cdef inline _try_write(self, object data):
161-
if self._get_write_buffer_size():
162-
# Don't try to write anything as we already have some
163-
# data in the write buffers.
164-
return
165-
166161
cdef:
167162
ssize_t written
168163
int fd
169164
Py_buffer py_buf
170165

171-
# Let's try to send the data right away.
172-
173166
fd = self._fileno()
174167
PyObject_GetBuffer(data, &py_buf, PyBUF_SIMPLE)
175168
written = system.send(fd, <char*>py_buf.buf, py_buf.len, 0)
@@ -185,21 +178,27 @@ cdef class UVStream(UVBaseTransport):
185178
exc = convert_error(-errno.errno)
186179
self._fatal_error(exc, True)
187180

181+
IF DEBUG:
182+
if written > 0:
183+
self._loop._debug_stream_write_tries += 1
184+
188185
return written
189186

190187
cdef inline _write(self, object data):
191188
cdef:
192189
int err
193-
int sent
194190
_StreamWriteContext ctx
195191

196192
self._ensure_alive()
197193

198-
sent = self._try_write(data)
199-
if sent > 0:
200-
if sent == len(data):
201-
return
202-
data = data[sent:]
194+
if not self._get_write_buffer_size():
195+
# Try to write without polling only when there is
196+
# no data in write buffers.
197+
sent = self._try_write(data)
198+
if sent > 0:
199+
if sent == len(data):
200+
return
201+
data = data[sent:]
203202

204203
ctx = _StreamWriteContext.new(self, data)
205204

uvloop/loop.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ cdef class Loop:
9797
readonly uint64_t _debug_stream_read_eof_cb_errors_total
9898
readonly uint64_t _debug_stream_read_errors_total
9999

100+
readonly uint64_t _debug_stream_write_tries
100101
readonly uint64_t _debug_stream_write_errors_total
101102
readonly uint64_t _debug_stream_write_ctx_total
102103
readonly uint64_t _debug_stream_write_ctx_cnt

uvloop/loop.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ cdef class Loop:
9797
self._debug_stream_shutdown_errors_total = 0
9898
self._debug_stream_listen_errors_total = 0
9999

100+
self._debug_stream_write_tries = 0
100101
self._debug_stream_write_errors_total = 0
101102
self._debug_stream_write_ctx_total = 0
102103
self._debug_stream_write_ctx_cnt = 0
@@ -759,6 +760,8 @@ cdef class Loop:
759760
print('--- Streams debug info: ---')
760761
print('Write errors: {}'.format(
761762
self._debug_stream_write_errors_total))
763+
print('Write without poll: {}'.format(
764+
self._debug_stream_write_tries))
762765
print('Write contexts: {: <8} | {}'.format(
763766
self._debug_stream_write_ctx_cnt,
764767
self._debug_stream_write_ctx_total))

0 commit comments

Comments
 (0)