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

Function run_async_client

example_code/item_076.py:452–480  ·  view source on GitHub ↗
(address)

Source from the content-addressed store, hash-verified

450
451print("Example 28")
452async def run_async_client(address):
453 # Wait for the server to listen before trying to connect
454 await asyncio.sleep(0.1)
455
456 streams = await asyncio.open_connection(*address) # New
457 client = AsyncConnection(*streams) # New
458
459 async with new_async_game(client, 1, 5, 3) as session:
460 results = [outcome async for outcome in session]
461
462 async with new_async_game(client, 10, 15, 12) as session:
463 async for outcome in session:
464 results.append(outcome)
465
466 async with new_async_game(client, 1, 3, 2) as session:
467 it = aiter(session)
468 while True:
469 try:
470 outcome = await anext(it)
471 except StopAsyncIteration:
472 break
473 else:
474 results.append(outcome)
475
476 _, writer = streams # New
477 writer.close() # New
478 await writer.wait_closed() # New
479
480 return results
481
482
483print("Example 29")

Callers 1

main_asyncFunction · 0.85

Calls 3

AsyncConnectionClass · 0.85
new_async_gameFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected