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

Make autoimport cache generation non-blocking #499

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
This one is for Murmel. Merry Xmas
  • Loading branch information
tkrabel committed Dec 22, 2023
commit 2f503ef3f63abe142b442d74c06fb33f87b23e85
18 changes: 18 additions & 0 deletions pylsp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import docstring_to_markdown
import jedi
import time
from functools import wraps

JEDI_VERSION = jedi.__version__

Expand Down Expand Up @@ -55,6 +57,22 @@ def run():
return wrapper


def throttle(seconds=1):
"""Throttles calls to a function evey `seconds` seconds."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs): # pylint: disable=inconsistent-return-statements
if not hasattr(wrapper, "last_call"):
wrapper.last_call = 0
if time.time() - wrapper.last_call >= seconds:
wrapper.last_call = time.time()
return func(*args, **kwargs)

return wrapper

return decorator


def find_parents(root, path, names):
"""Find files matching the given names relative to the given path.

Expand Down
18 changes: 1 addition & 17 deletions pylsp/plugins/_rope_task_handle.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
from __future__ import annotations

import logging
import time
from functools import wraps

from typing import Callable, ContextManager, List, Optional, Sequence

from rope.base.taskhandle import BaseJobSet, BaseTaskHandle

from pylsp.workspace import Workspace
from pylsp._utils import throttle

log = logging.getLogger(__name__)
Report = Callable[[str, int], None]


def throttle(seconds=1):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs): # pylint: disable=inconsistent-return-statements
if not hasattr(wrapper, "last_call"):
wrapper.last_call = 0
if time.time() - wrapper.last_call >= seconds:
wrapper.last_call = time.time()
return func(*args, **kwargs)

return wrapper

return decorator


class PylspJobSet(BaseJobSet):
count: int = 0
done: int = 0
Expand Down
2 changes: 1 addition & 1 deletion pylsp/plugins/rope_autoimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _reload_cache(
autoimport.generate_modules_cache(task_handle=task_handle)

def is_blocked(self):
return cache.thread and cache.thread.is_alive()
return self.thread and self.thread.is_alive()


@hookimpl
Expand Down
Loading