diff --git a/README.rst b/README.rst index 870b208ba..94cd79ab9 100644 --- a/README.rst +++ b/README.rst @@ -55,7 +55,7 @@ Here's an echo server with the ``asyncio`` API: async def main(): async with serve(echo, "localhost", 8765): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever asyncio.run(main()) diff --git a/compliance/test_server.py b/compliance/test_server.py index 92f895d92..5701e4485 100644 --- a/compliance/test_server.py +++ b/compliance/test_server.py @@ -21,7 +21,7 @@ async def echo(ws): async def main(): with websockets.serve(echo, HOST, PORT, max_size=2 ** 25, max_queue=1): try: - await asyncio.Future() + await asyncio.get_running_loop().create_future() # run forever except KeyboardInterrupt: pass diff --git a/docs/intro/tutorial1.rst b/docs/intro/tutorial1.rst index ff85003b5..6b32d47f6 100644 --- a/docs/intro/tutorial1.rst +++ b/docs/intro/tutorial1.rst @@ -195,7 +195,7 @@ Create an ``app.py`` file next to ``connect4.py`` with this content: async def main(): async with websockets.serve(handler, "", 8001): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever if __name__ == "__main__": diff --git a/docs/topics/broadcast.rst b/docs/topics/broadcast.rst index 1acb372d4..b6ddda734 100644 --- a/docs/topics/broadcast.rst +++ b/docs/topics/broadcast.rst @@ -273,10 +273,11 @@ Here's a message stream that supports multiple consumers:: class PubSub: def __init__(self): - self.waiter = asyncio.Future() + self.waiter = asyncio.get_running_loop().create_future() def publish(self, value): - waiter, self.waiter = self.waiter, asyncio.Future() + waiter = self.waiter + self.waiter = asyncio.get_running_loop().create_future() waiter.set_result((value, self.waiter)) async def subscribe(self): diff --git a/example/django/authentication.py b/example/django/authentication.py index f6dad0f55..83e128f07 100644 --- a/example/django/authentication.py +++ b/example/django/authentication.py @@ -23,7 +23,7 @@ async def handler(websocket): async def main(): async with websockets.serve(handler, "localhost", 8888): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever if __name__ == "__main__": diff --git a/example/echo.py b/example/echo.py index 2e47e52d9..d11b33527 100755 --- a/example/echo.py +++ b/example/echo.py @@ -9,6 +9,6 @@ async def echo(websocket): async def main(): async with serve(echo, "localhost", 8765): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever asyncio.run(main()) diff --git a/example/faq/health_check_server.py b/example/faq/health_check_server.py index 7b8bded77..6c7681e8a 100755 --- a/example/faq/health_check_server.py +++ b/example/faq/health_check_server.py @@ -17,6 +17,6 @@ async def main(): echo, "localhost", 8765, process_request=health_check, ): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever asyncio.run(main()) diff --git a/example/legacy/basic_auth_server.py b/example/legacy/basic_auth_server.py index d2efeb7e5..6f6020253 100755 --- a/example/legacy/basic_auth_server.py +++ b/example/legacy/basic_auth_server.py @@ -16,6 +16,6 @@ async def main(): realm="example", credentials=("mary", "p@ssw0rd") ), ): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever asyncio.run(main()) diff --git a/example/legacy/unix_server.py b/example/legacy/unix_server.py index 335039c35..5bfb66072 100755 --- a/example/legacy/unix_server.py +++ b/example/legacy/unix_server.py @@ -18,6 +18,6 @@ async def hello(websocket): async def main(): socket_path = os.path.join(os.path.dirname(__file__), "socket") async with websockets.unix_serve(hello, socket_path): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever asyncio.run(main()) diff --git a/example/quickstart/counter.py b/example/quickstart/counter.py index 566e12965..414919e04 100755 --- a/example/quickstart/counter.py +++ b/example/quickstart/counter.py @@ -43,7 +43,7 @@ async def counter(websocket): async def main(): async with websockets.serve(counter, "localhost", 6789): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever if __name__ == "__main__": asyncio.run(main()) diff --git a/example/quickstart/server.py b/example/quickstart/server.py index 31b182972..64d7adeb6 100755 --- a/example/quickstart/server.py +++ b/example/quickstart/server.py @@ -14,7 +14,7 @@ async def hello(websocket): async def main(): async with websockets.serve(hello, "localhost", 8765): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever if __name__ == "__main__": asyncio.run(main()) diff --git a/example/quickstart/server_secure.py b/example/quickstart/server_secure.py index de41d30dc..11db5fb3a 100755 --- a/example/quickstart/server_secure.py +++ b/example/quickstart/server_secure.py @@ -20,7 +20,7 @@ async def hello(websocket): async def main(): async with websockets.serve(hello, "localhost", 8765, ssl=ssl_context): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever if __name__ == "__main__": asyncio.run(main()) diff --git a/example/quickstart/show_time.py b/example/quickstart/show_time.py index a83078e8a..add226869 100755 --- a/example/quickstart/show_time.py +++ b/example/quickstart/show_time.py @@ -13,7 +13,7 @@ async def show_time(websocket): async def main(): async with websockets.serve(show_time, "localhost", 5678): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever if __name__ == "__main__": asyncio.run(main()) diff --git a/example/tutorial/step1/app.py b/example/tutorial/step1/app.py index 3b0fbd786..6ec1c60b8 100644 --- a/example/tutorial/step1/app.py +++ b/example/tutorial/step1/app.py @@ -58,7 +58,7 @@ async def handler(websocket): async def main(): async with websockets.serve(handler, "", 8001): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever if __name__ == "__main__": diff --git a/example/tutorial/step2/app.py b/example/tutorial/step2/app.py index 2693d4304..db3e36374 100644 --- a/example/tutorial/step2/app.py +++ b/example/tutorial/step2/app.py @@ -183,7 +183,7 @@ async def handler(websocket): async def main(): async with websockets.serve(handler, "", 8001): - await asyncio.Future() # run forever + await asyncio.get_running_loop().create_future() # run forever if __name__ == "__main__": diff --git a/experiments/broadcast/server.py b/experiments/broadcast/server.py index 9c9907b7f..b0407ba34 100644 --- a/experiments/broadcast/server.py +++ b/experiments/broadcast/server.py @@ -27,10 +27,11 @@ async def relay(queue, websocket): class PubSub: def __init__(self): - self.waiter = asyncio.Future() + self.waiter = asyncio.get_running_loop().create_future() def publish(self, value): - waiter, self.waiter = self.waiter, asyncio.Future() + waiter = self.waiter + self.waiter = asyncio.get_running_loop().create_future() waiter.set_result((value, self.waiter)) async def subscribe(self): diff --git a/src/websockets/legacy/protocol.py b/src/websockets/legacy/protocol.py index 19cee0e65..47f948b7a 100644 --- a/src/websockets/legacy/protocol.py +++ b/src/websockets/legacy/protocol.py @@ -664,7 +664,7 @@ async def send( return opcode, data = prepare_data(fragment) - self._fragmented_message_waiter = asyncio.Future() + self._fragmented_message_waiter = self.loop.create_future() try: # First fragment. await self.write_frame(False, opcode, data) @@ -709,7 +709,7 @@ async def send( return opcode, data = prepare_data(fragment) - self._fragmented_message_waiter = asyncio.Future() + self._fragmented_message_waiter = self.loop.create_future() try: # First fragment. await self.write_frame(False, opcode, data) diff --git a/src/websockets/legacy/server.py b/src/websockets/legacy/server.py index 7c24dd74a..d95bec4f6 100644 --- a/src/websockets/legacy/server.py +++ b/src/websockets/legacy/server.py @@ -897,7 +897,8 @@ class Serve: Awaiting :func:`serve` yields a :class:`WebSocketServer`. This object provides a :meth:`~WebSocketServer.close` method to shut down the server:: - stop = asyncio.Future() # set this future to exit the server + # set this future to exit the server + stop = asyncio.get_running_loop().create_future() server = await serve(...) await stop @@ -906,7 +907,8 @@ class Serve: :func:`serve` can be used as an asynchronous context manager. Then, the server is shut down automatically when exiting the context:: - stop = asyncio.Future() # set this future to exit the server + # set this future to exit the server + stop = asyncio.get_running_loop().create_future() async with serve(...): await stop