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
8 changes: 7 additions & 1 deletion cirq-google/cirq_google/engine/engine_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ 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):
return AsyncioExecutor._instance if AsyncioExecutor._instance else cls()


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

@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.

I'd suggest removing this cached_property decorator since there's no need to cache on each EngineClient instance as well as on the AsyncExecutor class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

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()
return AsyncioExecutor.instance()

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