bpo-32662: Implement Server.start_serving() and Server.serve_forever(… · python/cpython@c9070d0 (original) (raw)

`@@ -424,7 +424,7 @@ Creating connections

`

424

424

`Creating listening connections

`

425

425

`------------------------------

`

426

426

``

427

``

`-

.. coroutinemethod:: AbstractEventLoop.create_server(protocol_factory, host=None, port=None, *, family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None, reuse_port=None, ssl_handshake_timeout=None)

`

``

427

`+

.. coroutinemethod:: AbstractEventLoop.create_server(protocol_factory, host=None, port=None, *, family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None, reuse_port=None, ssl_handshake_timeout=None, start_serving=True)

`

428

428

``

429

429

`` Create a TCP server (socket type :data:~socket.SOCK_STREAM) bound to

``

430

430

` host and port.

`

`@@ -472,9 +472,15 @@ Creating listening connections

`

472

472

` for the SSL handshake to complete before aborting the connection.

`

473

473

``` 10.0 seconds if None (default).


`474`

`474`

``

``

`475`

``` +

* *start_serving* set to ``True`` (the default) causes the created server

``

476


 to start accepting connections immediately. When set to ``False``,

``

477

`` +

the user should await on :meth:Server.start_serving or

``

``

478

`` +

:meth:Server.serve_forever to make the server to start accepting

``

``

479

`+

connections.

`

``

480

+

475

481

` .. versionadded:: 3.7

`

476

482

``

477

``

`-

The ssl_handshake_timeout parameter.

`

``

483

`+

ssl_handshake_timeout and start_serving parameters.

`

478

484

``

479

485

` .. versionchanged:: 3.5

`

480

486

``

`@@ -490,7 +496,7 @@ Creating listening connections

`

490

496

` The host parameter can now be a sequence of strings.

`

491

497

``

492

498

``

493

``

`-

.. coroutinemethod:: AbstractEventLoop.create_unix_server(protocol_factory, path=None, *, sock=None, backlog=100, ssl=None, ssl_handshake_timeout=None)

`

``

499

`+

.. coroutinemethod:: AbstractEventLoop.create_unix_server(protocol_factory, path=None, *, sock=None, backlog=100, ssl=None, ssl_handshake_timeout=None, start_serving=True)

`

494

500

``

495

501

`` Similar to :meth:AbstractEventLoop.create_server, but specific to the

``

496

502

`` socket family :py:data:~socket.AF_UNIX.

``

`@@ -929,8 +935,26 @@ Server

`

929

935

``

930

936

` Server listening on sockets.

`

931

937

``

932

``

`` -

Object created by the :meth:AbstractEventLoop.create_server method and the

``

933

``

`` -

:func:start_server function. Don't instantiate the class directly.

``

``

938

`` +

Object created by :meth:AbstractEventLoop.create_server,

``

``

939

`` +

:meth:AbstractEventLoop.create_unix_server, :func:start_server,

``

``

940

`` +

and :func:start_unix_server functions. Don't instantiate the class

``

``

941

`+

directly.

`

``

942

+

``

943

`+

Server objects are asynchronous context managers. When used in an

`

``

944


 ``async with`` statement, it's guaranteed that the Server object is

``

945


 closed and not accepting new connections when the ``async with``

``

946

`+

statement is completed::

`

``

947

+

``

948

`+

srv = await loop.create_server(...)

`

``

949

+

``

950

`+

async with srv:

`

``

951

`+

some code

`

``

952

+

``

953

`+

At this point, srv is closed and no longer accepts new connections.

`

``

954

+

``

955

+

``

956

`+

.. versionchanged:: 3.7

`

``

957

`+

Server object is an asynchronous context manager since Python 3.7.

`

934

958

``

935

959

` .. method:: close()

`

936

960

``

`@@ -949,6 +973,54 @@ Server

`

949

973

``

950

974

` .. versionadded:: 3.7

`

951

975

``

``

976

`+

.. coroutinemethod:: start_serving()

`

``

977

+

``

978

`+

Start accepting connections.

`

``

979

+

``

980

`+

This method is idempotent, so it can be called when

`

``

981

`+

the server is already being serving.

`

``

982

+

``

983

`+

The new start_serving keyword-only parameter to

`

``

984

`` +

:meth:AbstractEventLoop.create_server and

``

``

985

`` +

:meth:asyncio.start_server allows to create a Server object

``

``

986

`+

that is not accepting connections right away. In which case

`

``

987

`` +

this method, or :meth:Server.serve_forever can be used

``

``

988

`+

to make the Server object to start accepting connections.

`

``

989

+

``

990

`+

.. versionadded:: 3.7

`

``

991

+

``

992

`+

.. coroutinemethod:: serve_forever()

`

``

993

+

``

994

`+

Start accepting connections until the coroutine is cancelled.

`

``

995


 Cancellation of ``serve_forever`` task causes the server

``

996

`+

to be closed.

`

``

997

+

``

998

`+

This method can be called if the server is already accepting

`

``

999


 connections. Only one ``serve_forever`` task can exist per

``

1000

`+

one Server object.

`

``

1001

+

``

1002

`+

Example::

`

``

1003

+

``

1004

`+

async def client_connected(reader, writer):

`

``

1005

`+

Communicate with the client with

`

``

1006

`+

reader/writer streams. For example:

`

``

1007

`+

await reader.readline()

`

``

1008

+

``

1009

`+

async def main(host, port):

`

``

1010

`+

srv = await asyncio.start_server(

`

``

1011

`+

client_connected, host, port)

`

``

1012

`+

await loop.serve_forever()

`

``

1013

+

``

1014

`+

asyncio.run(main('127.0.0.1', 0))

`

``

1015

+

``

1016

`+

.. versionadded:: 3.7

`

``

1017

+

``

1018

`+

.. method:: is_serving()

`

``

1019

+

``

1020


 Return ``True`` if the server is accepting new connections.

``

1021

+

``

1022

`+

.. versionadded:: 3.7

`

``

1023

+

952

1024

` .. coroutinemethod:: wait_closed()

`

953

1025

``

954

1026

`` Wait until the :meth:close method completes.

``

`@@ -958,6 +1030,11 @@ Server

`

958

1030

`` List of :class:socket.socket objects the server is listening to, or

``

959

1031

``` None if the server is closed.


`960`

`1032`

``

``

`1033`

`+

.. versionchanged:: 3.7

`

``

`1034`

``` +

Prior to Python 3.7 ``Server.sockets`` used to return the

``

1035

`+

internal list of server's sockets directly. In 3.7 a copy

`

``

1036

`+

of that list is returned.

`

``

1037

+

961

1038

``

962

1039

`Handle

`

963

1040

`------

`