-
Notifications
You must be signed in to change notification settings - Fork 184
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
Add event loop #2576
base: main
Are you sure you want to change the base?
Add event loop #2576
Conversation
The one who originally wrote the code is Raoul Wols Co-authored-by: Raoul Wols <[email protected]>
|
||
class EventLoopListener(sublime_plugin.EventListener): | ||
def on_exit(self) -> None: | ||
shutdown_event_loop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the event loop starts, when the LSP module is loaded,
and the loop ends when ST exits.
delete_this_example.py
Outdated
@@ -0,0 +1,27 @@ | |||
import sublime_plugin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file will be deleted.
delete_this_example.py
Outdated
view = await open_view(readme_file, w) | ||
view.run_command("append", { | ||
'characters': "LspExampleAsyncCommand added this" + '\n\n', | ||
}) | ||
await save_view(view) | ||
# the view is saved at this point and safe to be closed | ||
view.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These few lines illustrate one of the benefits.
On line 22, the view is guaranteed to be loaded.
After line 25, the view is guaranteed to be saved.
delete_this_example.py
Outdated
from .view_async import open_view, save_view | ||
from pathlib import Path | ||
|
||
class LspExampleAsyncCommand(sublime_plugin.TextCommand): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you want to try this command, here is a keybinding {"keys": ["primary+i"], "command": "lsp_example_async" }
or view.run_command('lsp_example_async')
if you want to run it from the ST console.
I have removed the unnecessary files, if you want to try the example, checkout the commit bd7cdae |
We run our logic either on the main or ST's async thread. When running a feature then on what thread those run technically? I'm thinking that it could open up for some interesting race conditions if we mix ST async's thread and features... |
I don't have the necessary knowledge to give the right answer, but I will give my understanding after spending time trying out the asyncio solution that rwols wrote on a different ST plugin. The event loop is started on a separate thread https://github.com/sublimelsp/LSP/pull/2576/files#diff-0f06b8214f4534bf9cfc50065f635b6bc543bc54f72946858a476319da98c4bbR29 . I did run into issues where promises got garbage collected before the finished. There will be issues, I don't doubt that. But as with any issue, a solution might appear over time, or the issue will stay. Here are some related/useful comments by others. sublimehq/sublime_text#3129:
|
This PR:
run_future
function .view_async.py
content could stay, but the file would need to be renamed and put in a proper place. (please say if it should stay).This PR will not do a huge refactor of the existing code,
instead it just add the first building block to use asyncio and futures. Nothing more, nothing less.
Why?
Currently we have
Promises
thanks to @rchl.Promises
allowed us to haveFutures
before we could useFutures
. Now that we can useFutures
, it will allow us to use the following asyncio apis:asyncio.gather(list_of_futures)
await a list of futuresasyncio.wait_for(future, 3)
# run future, but cancel the future it runs for more then 3 secondsfuture.cancel
cancel a future.Here is an example of what could be build by using, gather, wait_for, and cancel: