-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
bpo-32662: Implement Server.start_serving() and Server.serve_forever() #5312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,47 +157,106 @@ def _run_until_complete_cb(fut): | |
|
|
||
| class Server(events.AbstractServer): | ||
|
|
||
| def __init__(self, loop, sockets): | ||
| def __init__(self, loop, sockets, protocol_factory, ssl_context, backlog, | ||
| ssl_handshake_timeout): | ||
| self._loop = loop | ||
| self.sockets = sockets | ||
| self._sockets = sockets | ||
| self._active_count = 0 | ||
| self._waiters = [] | ||
| self._protocol_factory = protocol_factory | ||
| self._backlog = backlog | ||
| self._ssl_context = ssl_context | ||
| self._ssl_handshake_timeout = ssl_handshake_timeout | ||
| self._serving = False | ||
| self._serving_forever_fut = None | ||
|
|
||
| def __repr__(self): | ||
| return f'<{self.__class__.__name__} sockets={self.sockets!r}>' | ||
|
|
||
| def _attach(self): | ||
| assert self.sockets is not None | ||
| assert self._sockets is not None | ||
| self._active_count += 1 | ||
|
|
||
| def _detach(self): | ||
| assert self._active_count > 0 | ||
| self._active_count -= 1 | ||
| if self._active_count == 0 and self.sockets is None: | ||
| if self._active_count == 0 and self._sockets is None: | ||
| self._wakeup() | ||
|
|
||
| def _wakeup(self): | ||
| waiters = self._waiters | ||
| self._waiters = None | ||
| for waiter in waiters: | ||
| if not waiter.done(): | ||
| waiter.set_result(waiter) | ||
|
|
||
| def _start_serving(self): | ||
| if self._serving: | ||
| return | ||
| self._serving = True | ||
| for sock in self._sockets: | ||
| sock.listen(self._backlog) | ||
| self._loop._start_serving( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is loop's private method call.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we can't, without re-architecturing half of create server machinery and exposing a new event loop public API. I have no interest in doing that. And I don't see why this is a problem -- Loop and Server are designed to work together. Loop creates Server objects and knows everything about them. This is exactly the same relationship as between Transports and Loops. Also you can't use Server object from uvloop and vice versa, so I don't see any problem here either.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I don't insist. We have many tightly coupled classes in asyncio already. |
||
| self._protocol_factory, sock, self._ssl_context, | ||
| self, self._backlog, self._ssl_handshake_timeout) | ||
|
|
||
| def get_loop(self): | ||
| return self._loop | ||
|
|
||
| def is_serving(self): | ||
| return self._serving | ||
|
|
||
| @property | ||
| def sockets(self): | ||
| if self._sockets is None: | ||
| return [] | ||
| return list(self._sockets) | ||
|
|
||
| def close(self): | ||
| sockets = self.sockets | ||
| sockets = self._sockets | ||
| if sockets is None: | ||
| return | ||
| self.sockets = None | ||
| self._sockets = None | ||
|
|
||
| for sock in sockets: | ||
| self._loop._stop_serving(sock) | ||
|
|
||
| self._serving = False | ||
|
|
||
| if (self._serving_forever_fut is not None and | ||
| not self._serving_forever_fut.done()): | ||
| self._serving_forever_fut.cancel() | ||
| self._serving_forever_fut = None | ||
|
|
||
| if self._active_count == 0: | ||
| self._wakeup() | ||
|
|
||
| def get_loop(self): | ||
| return self._loop | ||
| async def start_serving(self): | ||
| self._start_serving() | ||
|
|
||
| def _wakeup(self): | ||
| waiters = self._waiters | ||
| self._waiters = None | ||
| for waiter in waiters: | ||
| if not waiter.done(): | ||
| waiter.set_result(waiter) | ||
| async def serve_forever(self): | ||
| if self._serving_forever_fut is not None: | ||
| raise RuntimeError( | ||
| f'server {self!r} is already being awaited on serve_forever()') | ||
| if self._sockets is None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should check
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we document this method as idempotent as related to server already accepting connections. I.e. it's fine to do this: await server.start_serving()
await server.serve_forever()The reason for this design is backwards-compatibility: I found the utility in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for explanation |
||
| raise RuntimeError(f'server {self!r} is closed') | ||
|
|
||
| self._start_serving() | ||
| self._serving_forever_fut = self._loop.create_future() | ||
|
|
||
| try: | ||
| await self._serving_forever_fut | ||
| except futures.CancelledError: | ||
| try: | ||
| self.close() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Moreover
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
OK, will do that.
Yes, I agree :( Need to fix that in 3.8.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's discuss it later. Actually at least in aiohttp we need stop listening first ( |
||
| await self.wait_closed() | ||
| finally: | ||
| raise | ||
| finally: | ||
| self._serving_forever_fut = None | ||
|
|
||
| async def wait_closed(self): | ||
| if self.sockets is None or self._waiters is None: | ||
| if self._sockets is None or self._waiters is None: | ||
| return | ||
| waiter = self._loop.create_future() | ||
| self._waiters.append(waiter) | ||
|
|
@@ -1059,7 +1118,8 @@ async def create_server( | |
| ssl=None, | ||
| reuse_address=None, | ||
| reuse_port=None, | ||
| ssl_handshake_timeout=None): | ||
| ssl_handshake_timeout=None, | ||
| start_serving=True): | ||
| """Create a TCP server. | ||
|
|
||
| The host parameter can be a string, in that case the TCP server is | ||
|
|
@@ -1149,12 +1209,14 @@ async def create_server( | |
| raise ValueError(f'A Stream Socket was expected, got {sock!r}') | ||
| sockets = [sock] | ||
|
|
||
| server = Server(self, sockets) | ||
| for sock in sockets: | ||
| sock.listen(backlog) | ||
| sock.setblocking(False) | ||
| self._start_serving(protocol_factory, sock, ssl, server, backlog, | ||
| ssl_handshake_timeout) | ||
|
|
||
| server = Server(self, sockets, protocol_factory, | ||
| ssl, backlog, ssl_handshake_timeout) | ||
| if start_serving: | ||
| server._start_serving() | ||
|
|
||
| if self._debug: | ||
| logger.info("%r is serving", server) | ||
| return server | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Backward incompatible change.
I pretty sure 99.9% of users newer modified the attribute but we should have a big warning about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was no point in modifying this attribute ever, but I agree, it needs to be documented. Will fix in the documentation.