Skip to content

Commit

Permalink
add more robust solution
Browse files Browse the repository at this point in the history
  • Loading branch information
alevenberg committed Oct 4, 2023
1 parent be1768b commit 0491908
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
1 change: 1 addition & 0 deletions speech/api/streaming_transcribe_coroutines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ int main(int argc, char* argv[]) try {
// operations, and dedicate a thread to it.
g::CompletionQueue cq;
auto runner = std::thread{[](auto cq) { cq.Run(); }, cq};
std::shared_ptr<void> auto_shutdown(nullptr, [&](void*) { cq.Shutdown(); runner.join(); });

// Run a streaming transcription. Note that `.get()` blocks until it
// completes.
Expand Down
8 changes: 2 additions & 6 deletions speech/api/streaming_transcribe_singlethread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,18 @@ class Handler : public std::enable_shared_from_this<Handler> {
};

int main(int argc, char** argv) try {
// Parse arguments before creating the thread. If this throws after
// the thread is created, the catch will never get run and the program
// will call `std::abort()`.
auto arguments = ParseArguments(argc, argv);

// Create a CompletionQueue to demux the I/O and other asynchronous
// operations, and dedicate a thread to it.
g::CompletionQueue cq;
auto runner = std::thread{[](auto cq) { cq.Run(); }, cq};
std::shared_ptr<void> auto_shutdown(nullptr, [&](void*) { cq.Shutdown(); runner.join(); });

// Create a Speech client with the default configuration.
auto client = speech::SpeechClient(speech::MakeSpeechConnection(
g::Options{}.set<g::GrpcCompletionQueueOption>(cq)));

// Create a handler for the stream and run it until closed.
auto handler = Handler::Create(cq, arguments);
auto handler = Handler::Create(cq, ParseArguments(argc, argv));
auto status = handler->Start(client).get();

// Shutdown the completion queue
Expand Down

0 comments on commit 0491908

Please sign in to comment.