Skip to content

Commit ed32d7e

Browse files
committed
Document asyncio transport APIs
1 parent afe7e21 commit ed32d7e

1 file changed

Lines changed: 170 additions & 0 deletions

File tree

Doc/library/asyncio.rst

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,176 @@ buffer size reaches the low-water mark.
242242
Transports
243243
----------
244244

245+
Transports are classed provided by :mod:`asyncio` in order to abstract
246+
various kinds of communication channels. You generally won't instantiate
247+
a transport yourself; instead, you will call a :class:`EventLoop` method
248+
which will create the transport and try to initiate the underlying
249+
communication channel, calling you back when it succeeds.
250+
251+
Once the communication channel is established, a transport is always
252+
paired with a :ref:`protocol <protocol>` instance. The protocol can
253+
then call the transport's methods for various purposes.
254+
255+
:mod:`asyncio` currently implements transports for TCP, UDP, SSL, and
256+
subprocess pipes. The methods available on a transport depend on
257+
the transport's kind.
258+
259+
Methods common to all transports
260+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
261+
262+
.. method:: close(self)
263+
264+
Close the transport. If the transport has a buffer for outgoing
265+
data, buffered data will be flushed asynchronously. No more data
266+
will be received. After all buffered data is flushed, the
267+
protocol's :meth:`connection_lost` method will be called with
268+
:const:`None` as its argument.
269+
270+
271+
.. method:: get_extra_info(name, default=None)
272+
273+
Return optional transport information. *name* is a string representing
274+
the piece of transport-specific information to get, *default* is the
275+
value to return if the information doesn't exist.
276+
277+
This method allows transport implementations to easily expose
278+
channel-specific information.
279+
280+
Methods of readable streaming transports
281+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
282+
283+
.. method:: pause_reading()
284+
285+
Pause the receiving end of the transport. No data will be passed to
286+
the protocol's :meth:`data_received` method until meth:`resume_reading`
287+
is called.
288+
289+
.. method:: resume_reading()
290+
291+
Resume the receiving end. The protocol's :meth:`data_received` method
292+
will be called once again if some data is available for reading.
293+
294+
Methods of writable streaming transports
295+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
296+
297+
.. method:: write(data)
298+
299+
Write some *data* bytes to the transport.
300+
301+
This method does not block; it buffers the data and arranges for it
302+
to be sent out asynchronously.
303+
304+
.. method:: writelines(list_of_data)
305+
306+
Write a list (or any iterable) of data bytes to the transport.
307+
This is functionally equivalent to calling :meth:`write` on each
308+
element yielded by the iterable, but may be implemented more efficiently.
309+
310+
.. method:: write_eof()
311+
312+
Close the write end of the transport after flushing buffered data.
313+
Data may still be received.
314+
315+
This method can raise :exc:`NotImplementedError` if the transport
316+
(e.g. SSL) doesn't support half-closes.
317+
318+
.. method:: can_write_eof()
319+
320+
Return :const:`True` if the transport supports :meth:`write_eof`,
321+
:const:`False` if not.
322+
323+
.. method:: abort()
324+
325+
Close the transport immediately, without waiting for pending operations
326+
to complete. Buffered data will be lost. No more data will be received.
327+
The protocol's :meth:`connection_lost` method will eventually be
328+
called with :const:`None` as its argument.
329+
330+
.. method:: set_write_buffer_limits(high=None, low=None)
331+
332+
Set the *high*- and *low*-water limits for write flow control.
333+
334+
These two values control when call the protocol's
335+
:meth:`pause_writing` and :meth:`resume_writing` methods are called.
336+
If specified, the low-water limit must be less than or equal to the
337+
high-water limit. Neither *high* nor *low* can be negative.
338+
339+
The defaults are implementation-specific. If only the
340+
high-water limit is given, the low-water limit defaults to a
341+
implementation-specific value less than or equal to the
342+
high-water limit. Setting *high* to zero forces *low* to zero as
343+
well, and causes :meth:`pause_writing` to be called whenever the
344+
buffer becomes non-empty. Setting *low* to zero causes
345+
:meth:`resume_writing` to be called only once the buffer is empty.
346+
Use of zero for either limit is generally sub-optimal as it
347+
reduces opportunities for doing I/O and computation
348+
concurrently.
349+
350+
.. method:: get_write_buffer_size()
351+
352+
Return the current size of the output buffer used by the transport.
353+
354+
Methods of datagram transports
355+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
356+
357+
.. method:: sendto(data, addr=None)
358+
359+
Send the *data* bytes to the remote peer given by *addr* (a
360+
transport-dependent target address). If *addr* is :const:`None`, the
361+
data is sent to the target address given on transport creation.
362+
363+
This method does not block; it buffers the data and arranges for it
364+
to be sent out asynchronously.
365+
366+
.. method:: abort()
367+
368+
Close the transport immediately, without waiting for pending operations
369+
to complete. Buffered data will be lost. No more data will be received.
370+
The protocol's :meth:`connection_lost` method will eventually be
371+
called with :const:`None` as its argument.
372+
373+
Methods of subprocess transports
374+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
375+
376+
.. method:: get_pid()
377+
378+
Return the subprocess process id as an integer.
379+
380+
.. method:: get_returncode()
381+
382+
Return the subprocess returncode as an integer or :const:`None`
383+
if it hasn't returned, similarly to the
384+
:attr:`subprocess.Popen.returncode` attribute.
385+
386+
.. method:: get_pipe_transport(fd)
387+
388+
Return the transport for the communication pipe correspondong to the
389+
integer file descriptor *fd*. The return value can be a readable or
390+
writable streaming transport, depending on the *fd*. If *fd* doesn't
391+
correspond to a pipe belonging to this transport, :const:`None` is
392+
returned.
393+
394+
.. method:: send_signal(signal)
395+
396+
Send the *signal* number to the subprocess, as in
397+
:meth:`subprocess.Popen.send_signal`.
398+
399+
.. method:: terminate()
400+
401+
Ask the subprocess to stop, as in :meth:`subprocess.Popen.terminate`.
402+
This method is an alias for the :meth:`close` method.
403+
404+
On POSIX systems, this method sends SIGTERM to the subprocess.
405+
On Windows, the Windows API function TerminateProcess() is called to
406+
stop the subprocess.
407+
408+
.. method:: kill(self)
409+
410+
Kill the subprocess, as in :meth:`subprocess.Popen.kill`
411+
412+
On POSIX systems, the function sends SIGKILL to the subprocess.
413+
On Windows, this method is an alias for :meth:`terminate`.
414+
245415

246416
.. _sync:
247417

0 commit comments

Comments
 (0)