@@ -1616,7 +1616,7 @@ cdef class Loop:
1616
1616
""" Remove a writer callback."""
1617
1617
self ._remove_writer(fd)
1618
1618
1619
- def sock_recv (self , sock , n ):
1619
+ async def sock_recv(self , sock, n):
1620
1620
""" Receive data from the socket.
1621
1621
1622
1622
The return value is a bytes object representing the data received.
@@ -1627,11 +1627,21 @@ cdef class Loop:
1627
1627
"""
1628
1628
if self ._debug and sock.gettimeout() != 0 :
1629
1629
raise ValueError (" the socket must be non-blocking" )
1630
+
1631
+ try :
1632
+ data = sock.recv(n)
1633
+ except (BlockingIOError, InterruptedError):
1634
+ pass
1635
+ else :
1636
+ IF DEBUG:
1637
+ self ._sock_try_read_total += 1
1638
+ return data
1639
+
1630
1640
fut = self ._new_future()
1631
1641
self ._sock_recv(fut, 0 , sock, n)
1632
- return fut
1642
+ return await fut
1633
1643
1634
- def sock_sendall (self , sock , data ):
1644
+ async def sock_sendall(self , sock, data):
1635
1645
""" Send data to the socket.
1636
1646
1637
1647
The socket must be connected to a remote socket. This method continues
@@ -1644,12 +1654,29 @@ cdef class Loop:
1644
1654
"""
1645
1655
if self ._debug and sock.gettimeout() != 0 :
1646
1656
raise ValueError (" the socket must be non-blocking" )
1647
- fut = self ._new_future()
1648
- if data:
1649
- self ._sock_sendall(fut, 0 , sock, data)
1657
+
1658
+ if not data:
1659
+ return
1660
+
1661
+ try :
1662
+ n = sock.send(data)
1663
+ except (BlockingIOError, InterruptedError):
1664
+ pass
1650
1665
else :
1651
- fut.set_result(None )
1652
- return fut
1666
+ IF DEBUG:
1667
+ # This can be a partial success, i.e. only part
1668
+ # of the data was sent
1669
+ self ._sock_try_write_total += 1
1670
+
1671
+ if n == len (data):
1672
+ return
1673
+ if not isinstance (data, memoryview):
1674
+ data = memoryview(data)
1675
+ data = data[n:]
1676
+
1677
+ fut = self ._new_future()
1678
+ self ._sock_sendall(fut, 0 , sock, data)
1679
+ return await fut
1653
1680
1654
1681
def sock_accept (self , sock ):
1655
1682
""" Accept a connection.
0 commit comments