Skip to content

Make AsyncioExecutor a shared resource across EngineClients #5976

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

Merged
merged 8 commits into from
Jan 5, 2023
13 changes: 11 additions & 2 deletions cirq-google/cirq_google/engine/engine_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def submit(self, func: Callable[..., Awaitable[_R]], *args, **kw) -> duet.Awaita
future = asyncio.run_coroutine_threadsafe(func(*args, **kw), self.loop)
return duet.AwaitableFuture.wrap(future)

_instance = None

@classmethod
def instance(cls):
if cls._instance is None:
cls._instance = cls()
return cls._instance


class EngineClient:
"""Client for the Quantum Engine API handling protos and gRPC client.
Expand Down Expand Up @@ -122,9 +130,10 @@ def __init__(

self._service_args = service_args

@cached_property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, need to add back @property since other places in the code call this like a property instead of a method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Fixed and tested; thanks!

def _executor(self) -> AsyncioExecutor:
return AsyncioExecutor()
# We must re-use a single Executor due to multi-threading issues in gRPC
# clients: https://github.com/grpc/grpc/issues/25364.
return AsyncioExecutor.instance()

@cached_property
def grpc_client(self) -> quantum.QuantumEngineServiceAsyncClient:
Expand Down