Skip to content

Commit

Permalink
Create futures with create_future.
Browse files Browse the repository at this point in the history
This is the preferred way to create Futures in asyncio.
  • Loading branch information
aaugustin committed Jan 14, 2024
1 parent 7b522ec commit 35bc7dd
Show file tree
Hide file tree
Showing 18 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion compliance/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/intro/tutorial1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
5 changes: 3 additions & 2 deletions docs/topics/broadcast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion example/django/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
2 changes: 1 addition & 1 deletion example/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion example/faq/health_check_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion example/legacy/basic_auth_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion example/legacy/unix_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion example/quickstart/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion example/quickstart/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion example/quickstart/server_secure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion example/quickstart/show_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion example/tutorial/step1/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
2 changes: 1 addition & 1 deletion example/tutorial/step2/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
5 changes: 3 additions & 2 deletions experiments/broadcast/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/websockets/legacy/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/websockets/legacy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 35bc7dd

Please sign in to comment.