How to use THttpServer in a standalone application

Hello,

I am attempting to run an httpserver from an application that links to ROOT, but the server doesn’t seem to work correctly out of the box.

In the ROOT terminal it is enough to run auto server = new THttpServer(); in order to start the server, it will run in the background.

In my application I am running the server as follows:

#include <THttpServer.h>
#include <unistd.h>

int main() {
    auto server = new THttpServer();

    unsigned int microsecond = 1000000;
    usleep(30 * microsecond);  // sleeps for 3 second
}

I need to add some sleep afterwards because otherwise it would exit after creating the server. When I run the server like this, It doesn’t respond to http requests, they remain stuck on “pending”, so the server is running but not working correctly. I have also tried to start the server in its own thread with std::thread with the same results.

Try with:

#include <THttpServer.h>
#include <TApplication.h>

int main(int argc, char **argv)
{
   TApplication myapp("myapp", &argc, argv);
   auto server = new THttpServer();
   myapp.Run();
   return 0;
}

Thanks! with this solution I can run it, but its blocking the main thread (as I am sure it was intended). I would like however to run it in the background. I am trying to do this via std::thread but it doesn’t seem to be striaght forward, is there any recommended way to run a TApplication in the background?

Thanks a lot.

TApplication is the main event loop for ROOT, and AFAIK it should be in the main thread…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.