Use _LazyImport for grpcio package#5954
Merged
HideakiImamura merged 1 commit intooptuna:masterfrom Jan 31, 2025
Merged
Conversation
6afa96e to
bc29f94
Compare
Co-authored-by: Shuhei Watanabe <47781922+nabenabe0928@users.noreply.github.com>
bc29f94 to
a24a13b
Compare
Member
Author
|
I confirmed that this PR and #5915 resolves the issue reported at #5947. |
HideakiImamura
approved these changes
Jan 31, 2025
Member
HideakiImamura
left a comment
There was a problem hiding this comment.
Looks very good. I confirmed that the current version pass the optuna import w/o grpcio and raises an error with old grpcio and without it. I also confirmed that the difference between the previous server.py and the current servicer.py is reasonable as follows.
3d2
< from concurrent.futures import ThreadPoolExecutor
6a6
> from typing import TYPE_CHECKING
9c9
< from optuna._experimental import experimental_func
---
> from optuna._imports import _LazyImport
14d13
< from optuna.storages._grpc.grpc_imports import _imports
20,25c19,20
< if _imports.is_successful():
< from optuna.storages._grpc.grpc_imports import api_pb2
< from optuna.storages._grpc.grpc_imports import api_pb2_grpc
< from optuna.storages._grpc.grpc_imports import grpc
< from optuna.storages._grpc.grpc_imports import StorageServiceServicer
< else:
---
> if TYPE_CHECKING:
> import grpc
27,28c22,27
< class StorageServiceServicer: # type: ignore
< pass
---
> from optuna.storages._grpc.auto_generated import api_pb2
> from optuna.storages._grpc.auto_generated import api_pb2_grpc
> else:
> api_pb2 = _LazyImport("optuna.storages._grpc.auto_generated.api_pb2")
> api_pb2_grpc = _LazyImport("optuna.storages._grpc.auto_generated.api_pb2_grpc")
> grpc = _LazyImport("grpc")
35c34
< class OptunaStorageProxyService(StorageServiceServicer):
---
> class OptunaStorageProxyService(api_pb2_grpc.StorageServiceServicer):
37d35
< _imports.check()
431,483d428
<
<
< def make_server(
< storage: BaseStorage, host: str, port: int, thread_pool: ThreadPoolExecutor | None = None
< ) -> grpc.Server:
< server = grpc.server(thread_pool or ThreadPoolExecutor(max_workers=10))
< api_pb2_grpc.add_StorageServiceServicer_to_server(
< OptunaStorageProxyService(storage), server
< ) # type: ignore
< server.add_insecure_port(f"{host}:{port}")
< return server
<
<
< @experimental_func("4.2.0")
< def run_grpc_proxy_server(
< storage: BaseStorage,
< *,
< host: str = "localhost",
< port: int = 13000,
< thread_pool: ThreadPoolExecutor | None = None,
< ) -> None:
< """Run a gRPC server for the given storage URL, host, and port.
<
< Example:
<
< Run this server with the following way:
<
< .. code::
<
< from optuna.storages import run_grpc_proxy_server
< from optuna.storages import get_storage
<
< storage = get_storage("mysql+pymysql://<user>:<pass>@<host>/<dbname>[?<options>]")
< run_grpc_proxy_server(storage, host="localhost", port=13000)
<
< Please refer to the client class :class:`~optuna.storages.GrpcStorageProxy` for
< the client usage. Please use :func:`~optuna.storages.get_storage` instead of
< :class:`~optuna.storages.RDBStorage` since ``RDBStorage`` by itself does not use cache in
< process and it may cause significant slowdown.
<
< Args:
< storage: A storage object to proxy.
< host: Hostname to listen on.
< port: Port to listen on.
< thread_pool:
< Thread pool to use for the server. If :obj:`None`, a default thread pool
< with 10 workers will be used.
< """
< server = make_server(storage, host, port, thread_pool)
< server.start()
< _logger.info(f"Server started at {host}:{port}")
< _logger.info("Listening...")
< server.wait_for_termination()
HideakiImamura
added a commit
to HideakiImamura/optuna
that referenced
this pull request
Feb 12, 2025
Use `_LazyImport` for grpcio package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Resolves #5947
Description of the changes
The approach of this PR is very similar with #5915, but avoid defining the inner class.
Here is a high-level overview of the changes.
Before:
After: