Use DNSResolver as an async context manager
masterFor short-lived scenarios like tests or one-off scripts where automatic cleanup is required, DNSResolver supports the async context manager protocol. Using async with ensures resolver.close() is called automatically upon exiting the block.
Note: This is generally discouraged for production applications. DNSResolver instances are designed to be long-lived and reused. Frequent creation and destruction of resolvers adds unnecessary overhead.
async with aiodns.DNSResolver() as resolver:
result = await resolver.query_dns('example.com', 'A')
# resolver.close() is called automatically when exiting the context