Skip to content

implement asychronous callbacks and commands - #2031

Closed
Vizonex wants to merge 2 commits into
fastapi:masterfrom
Vizonex:asyncio-support
Closed

Vizonex wants to merge 2 commits into
fastapi:masterfrom
Vizonex:asyncio-support

Conversation

@Vizonex

@Vizonex Vizonex commented Nov 26, 2026

Copy link
Copy Markdown

Pull Request

This PR is related to and closes #633

Discussion:

Description

This PR allows callbacks and commands to be used asynchonously on the same event-loop, I have an example right here that uses something that I have been working on behind the scenes which is windows support with uvloop.

Here I have made a dummy script that does dns requests using aiodns.

from enum import StrEnum

import uvloop  # I'll be using the brand new windows supported uvloop 
# for this example but winloop will work more or less the same.
from aiodns import DNSResolver
from typer import Typer


class QueryType(StrEnum):
    A = "A"
    AAAA = "AAAA"
    ANY = "ANY"
    CAA = "CAA"
    CNAME = "CNAME"
    MX = "MX"
    NAPTR = "NAPTR"
    SOA = "SOA"
    SRV = "SRV"
    TXT = "TXT"




resolver : DNSResolver | None = None
app = Typer(loop_factory=uvloop.new_event_loop)

@app.callback()
async def callback(servers: list[str] | None = None):
    """Sets up nameserevrs"""
    # Although were not doing anything async in here having
    # the eventloop is helpful.
    global resolver
    resolver = DNSResolver(nameservers=servers)

@app.command()
async def dns(host:str, query: QueryType = QueryType.A):
    """DNS Queries for a random host"""
    async with resolver:
        results = await resolver.query_dns(host, query.value)
    print(results)


if __name__ == "__main__":
    app()

There is minimal requirements thanks to 3.11+ support all that is required is a runner.
I'll turn this into a draft for now until I have made some tests and other items.
My goal is not to waste anybody's time hence making this into a draft.

AI Disclaimer

AI transcript

Checklist

  • This PR links to a GitHub Discussion for the proposed code change.
  • I added tests for the change.
  • The new or updated tests fail on the main branch and pass on this PR.
  • Coverage stays at 100%.
  • The documentation explains the change if needed.

@svlandeg svlandeg left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closing, as this doesn't follow the contribution guidelines or the PR template. It claims to "close #633" with #633 being a PR, which makes no sense.

@svlandeg svlandeg closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants