обрабатывать запросы с помощью пула пользовательских потоков grpc - PullRequest
0 голосов
/ 18 октября 2019

Я просматривал этот async_server пример. Я заинтересован в следующем фрагменте кода:

while (true) {
  // Block waiting to read the next event from the completion queue. The
  // event is uniquely identified by its tag, which in this case is the
  // memory address of a CallData instance.
  // The return value of Next should always be checked. This return value
  // tells us whether there is any kind of event or cq_ is shutting down.
  GPR_ASSERT(cq_->Next(&tag, &ok));
  GPR_ASSERT(ok);
  /*
    How to enqueue this request on an existing thread pool
    For example,
    pool.enqueue() {
         /* what to enqueue so that when one worker thread of my thread pool
         pop one requests it can decide what handler to execute */
    }
  */

  // do not process request in this thread, commenting it
  // static_cast<CallData*>(tag)->Proceed();
}

В моем проекте вышеуказанный цикл выполняется одним выделенным потоком, и этот поток в основном помещает запросы в очередь thread-safe. Рабочие потоки в пуле извлекают объекты запросов из очереди thread_safe и параллельно обрабатывают запросы в многоядерной среде.

У меня есть несколько простых функций-обработчиков, и они должны запускаться для конкретного запроса, выведенного из очереди рабочим потоком. Объект, который будет помещен в очередь, должен иметь достаточно информации, чтобы рабочий поток мог решить, какой обработчик выполнить.

Например;Функции-обработчики могут быть очень простыми задачами, связанными с процессором.

void handler1( .... some argument ) {
    // a CPU bound for loop
    // reply someting to the client }

Мой вопрос: Как поместить информацию тега в пользовательскую очередь thread_safe, предполагая, что очередь thread_safe обеспечиваетследующий API:

enqueue(someObj task) {
     // task should have tag information required for the worker threads to select 
     // proper handler function.
     // I need to know the following, what should I push to the queue or what 
     // other code changes I need to do to push something meaningful for the 
     // worker threads. Should I create multiple services? Or can I handle 
     // multiple handler functions by having the same service. 
}

Когда я говорю handler function, я имею в виду следующее в отношении существующего кода в ссылке async_server .

    // inside proceed() function
    // The actual processing.

    std::string prefix("Hello ");
    reply_.set_message(prefix + request_.name());

Спасибо.

...