diff --git a/cirq-google/cirq_google/engine/engine_client.py b/cirq-google/cirq_google/engine/engine_client.py index 521fa600669..d9520ea122d 100644 --- a/cirq-google/cirq_google/engine/engine_client.py +++ b/cirq-google/cirq_google/engine/engine_client.py @@ -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. @@ -122,9 +130,11 @@ def __init__( self._service_args = service_args - @cached_property + @property 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: