Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move JsonRPCServer.start_io to high-level asyncio API #506

Merged
merged 2 commits into from
Oct 26, 2024

Conversation

alcarney
Copy link
Collaborator

@alcarney alcarney commented Oct 25, 2024

Description (e.g. "Related to ...", etc.)

Now that we have end-to-end tests around all 3 supported transports (stdio, tcp, websockets). This PR starts the process of migrating the JsonRPCServer side of pygls over to the high-level asyncio APIs.

A new pygls.io_ module has been introduced to hold the I/O code that's common between the client and server sides of pygls, including

  • run and run_async functions, which host the main sync/async message processing loop
  • (Eventually) all reader/writer adapters. Currently there is just StdinAsyncReader which allows a server to read from stdin asynchronously
  • Reader and AsyncReader typing protocols which capture the expected shape of the message source.

The client's start_io and start_tcp as well as the server's start_io methods have also been migrated to use the new run_async function.

Code review checklist (for code reviewer to complete)

  • Pull request represents a single change (i.e. not fixing disparate/unrelated things in a single PR)
  • Title summarizes what is changing
  • Commit messages are meaningful (see this for details)
  • Tests have been included and/or updated, as appropriate
  • Docstrings have been included and/or updated, as appropriate
  • Standalone docs have been updated accordingly

Automated linters

You can run the lints that are run on CI locally with:

poetry install --all-extras --with dev
poetry run poe lint

This commit introduces a new module `pygls.io_` which contains the
aspects of running a message processing loop that is common to both
client and servers.

It contains a few protocol definitions `Reader` and `AsyncReader`
which capture the expectations pygls will have when it tries to read
messages from a source.

It also provides `run` and `run_async` functions, both of which are
modelled on the old `aio_readline` method from the `pygls.server`
module. As their names suggest, these functions are responsible for
running a message processing loop synchronously or asynchronously.

Lastly the new `pygls.io_` module provides a `StdinAsyncReader` which
allows messages to be read from stdin asynchronously, by executing the
reads in a threadpool. (Just like the original `aio_readline`
function)

This commit also refactors the `JsonRPCServer` to make use of the new
`run_async` function in its `start_io` method, removing one use of the
lower-level asyncio API
@alcarney alcarney requested a review from tombh October 25, 2024 19:24
@alcarney alcarney changed the title Move JsonRPCServer to high-level asyncio API for start_io Move JsonRPCServer.start_io to high-level asyncio API Oct 25, 2024
Copy link
Collaborator

@tombh tombh left a comment

Choose a reason for hiding this comment

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

Great stuff, each PR building on previous PRs and improving the core 🤓

@alcarney
Copy link
Collaborator Author

Thanks! :)

@alcarney alcarney merged commit c494b42 into openlawlibrary:main Oct 26, 2024
16 checks passed
@alcarney alcarney deleted the server-stdio branch October 26, 2024 09:41
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