question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adopt new Python extensions non-blocking API for interpreter disocvery

See original GitHub issue

Three discovery APIs are being added for Jupyter which is meant to replace existing getInterpreters() API: https://github.com/microsoft/vscode-python/pull/17452

type PythonApiForJupyterExtension = {
     ...
    /**
     * IInterpreterService
     */
    readonly refreshPromise: Promise<void> | undefined;
    /**
     * IInterpreterService
     */
    readonly onDidChangeInterpreters: Event<PythonEnvironmentsChangedEvent>;
    /**
     * Equivalent to getInterpreters() in IInterpreterService
     */
    getKnownInterpreters(resource?: Uri): PythonEnvironment[];
    /**
     * Equivalent to getAllInterpreters() in IInterpreterService
     */
    getInterpreters(resource?: Uri): Promise<PythonEnvironment[]>;
    ...

Previous behavior:

Existing getInterpreters() is an asynchronus blocking API, so it waits until all of discovery is completed the first time it is called. Subsequent calls return instantaneously from cache if the same resource is queried again.

New behavior:

  • New getKnownInterpreters() simply returns whatever is currently in cache. It is useful when we do not need all of the interpreters at once, which is usually the case. In the Python extension we are able to replace all instances of getInterpreters() with getKnownInterpreters(). Do note that the kind info returned by this API may change as refresh progresses.
  • onDidChangeInterpreters fires when an environment gets added/removed/updated in cache. Event properties tracks the old and new environments accordingly.
  • Python extension triggers a refresh in background when the extension loads for the first time, which is when refreshPromise is set. It can be used to implement now deprecated getInterpreters() in the following way:
    /** Gets all interpreters **/
    public async getInterpreters(resource?: Uri): Promise<PythonEnvironment[]> {
        await this.refreshPromise; // Wait for the refresh to finish so that the cache contains all environments.
        return getKnownInterpreters(resource); // Return from cache.
    }
  • getInterpreterDetails() is still available as before, and can be used to get complete and reliable information on environments. As it is a blocking call, it’s recommend to only use it if complete information is needed, which is generally only the case for selected interpreters.

You can the see the new API in action here in our dynamic quickpick https://github.com/microsoft/vscode-python/pull/17043#issue-975156935.

Note: Any internal interpreter list caching that was recently added should go away once onDidChangeInterpreters events are available.

Changes regarding quickpick API

We also used to expose getSuggestions API for the quickpick, which has the same problem as getInterpreters(). The following non-blocking API is being added to replace it:

getKnownSuggestions(resource: Resource): Promise<IInterpreterQuickPickItem[]>;

It can be used in combination of refreshPromise to get all suggestions. Let me know if you have any questions! 😄

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:32 (32 by maintainers)

github_iconTop GitHub Comments

1reaction
karrtikrcommented, Oct 24, 2022

Makes sense, not a problem.

1reaction
DonJayamannecommented, Oct 23, 2022

Closing this as completed, the new API has been completely adopted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add non-blocking discovery APIs for Jupyter by karrtikr · Pull ...
Add non-blocking discovery APIs for Jupyter #17452 ... Adopt new Python extensions non-blocking API for interpreter disocvery microsoft/vscode-jupyter#7583.
Read more >
Extending and Embedding the Python Interpreter — Python ...
This document describes how to write modules in C or C++ to extend the Python interpreter with new modules. Those modules can not...
Read more >
Interpreter Discovery - Ansible Documentation
Detects the target OS platform, distribution, and version, then consults a table listing the correct Python interpreter and path for each platform/distribution/ ...
Read more >
Python Asyncio: The Complete Guide
Discover how to use the Python asyncio module including how to define, create, and run new coroutines and how to use non-blocking I/O....
Read more >
Visual Studio Code September 2022
New environments API for extension authors. The Python API now provides a way for extensions to work with Python environments available in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found