MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / new_async_game

Function new_async_game

example_code/item_076.py:367–383  ·  view source on GitHub ↗
(connection, lower, upper, secret)

Source from the content-addressed store, hash-verified

365print("Example 22")
366@contextlib.asynccontextmanager # Changed
367async def new_async_game(connection, lower, upper, secret): # Changed
368 print(
369 f"Guess a number between {lower} and {upper}!"
370 f" Shhhhh, it's {secret}."
371 )
372 await connection.send(f"PARAMS {lower} {upper}") # Changed
373 try:
374 yield AsyncClientSession(
375 connection.send,
376 connection.receive,
377 secret,
378 )
379 finally:
380 # Make it so the output printing is in
381 # the same order as the threaded version.
382 await asyncio.sleep(0.1)
383 await connection.send("CLEAR") # Changed
384
385
386print("Example 23")

Callers 1

run_async_clientFunction · 0.85

Calls 2

AsyncClientSessionClass · 0.85
sendMethod · 0.45

Tested by

no test coverage detected