Skip to content

Commit

Permalink
fix: use import extensions (#2204)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanFM authored Mar 19, 2021
1 parent 7b53b35 commit f7e3355
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
10 changes: 3 additions & 7 deletions jina/executors/indexers/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from . import BaseVectorIndexer
from ..decorators import batching
from ...helper import cached_property
from ...importer import ImportExtensions


class BaseNumpyIndexer(BaseVectorIndexer):
Expand Down Expand Up @@ -520,11 +521,6 @@ def _cosine(self, cached_A, raw_B):

@batching(merge_over_axis=1, slice_on=2)
def _cdist(self, *args, **kwargs):
try:
with ImportExtensions(required=True):
from scipy.spatial.distance import cdist

return cdist(*args, **kwargs, metric=self.metric)
except ModuleNotFoundError:
raise ModuleNotFoundError(
f'your metric {self.metric} requires scipy, but scipy is not found'
)
return cdist(*args, **kwargs, metric=self.metric)
19 changes: 10 additions & 9 deletions jina/logging/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from functools import wraps
from typing import Optional

from ..importer import ImportExtensions
from ..helper import colored, get_readable_size, get_readable_time

if False:
# fix type-hint complain for sphinx and flake
from . import JinaLogger
from ..logging import JinaLogger


def used_memory(unit: int = 1024 * 1024 * 1024) -> float:
Expand All @@ -21,18 +22,18 @@ def used_memory(unit: int = 1024 * 1024 * 1024) -> float:
:param unit: Unit of the memory, default in Gigabytes.
:return: Memory usage of the current process.
"""
try:
with ImportExtensions(required=False):
import resource

return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / unit
except ModuleNotFoundError:
from . import default_logger

default_logger.error(
'module "resource" can not be found and you are likely running it on Windows, '
'i will return 0'
)
return 0
from . import default_logger

default_logger.error(
'module "resource" can not be found and you are likely running it on Windows, '
'i will return 0'
)
return 0


def used_memory_readable() -> str:
Expand Down

0 comments on commit f7e3355

Please sign in to comment.