-1

I have a c++ program on a raspberry pi (headless setup) that records data from an ADC every minute over a period of days and stores them in a file. I want to add an web interface to it that launches possibly many instances of the program, keeps track of how far along they are till completion, view the data possibly in a chart and download the recorded data after completion. Once launched, I want the instances to be running even after the client closes the web interface. What is the best way of doing this? The most troubling part of this for me seems to be maintaining a long running task in the background within a webserver which is conceptually short-lived (a session ends when the client closes the tab in the browser)


What I have considered so far

  1. Ready-made web servers like nginx and apache with CGI: But the conceptual problem is the same. If I arrange it so that a cgi application executes when a button is clicked, because the cgi application has to return before the webserver can send a response to the client, I can't see how it can do the job. Even if I launch it as a child process and detach it, then I will lose control over it for termination, checking progress etc. If somehow it is possible, is there a good cgi/fastcgi library in c++?
  2. Something like the wt framework: Same problem. WRun creates an object of the wtapplication class for each "session" (client) and frees them when the client disconnects or after a period of inactivity. So I'd lose track of my launched programs when the client that launches the program disconnects.

Is there any other way of doing this? Or some kind of solution that is employed in this scenario? Any other libraries/frameworks/solutions to consider?

3
  • Honestly, I cheat. Dump the data into a database with C++ and use something else to transform the database into a webpage. It's do-able in C++, but much much easier in any of the languages designed with web pages in mind. Commented Mar 2, 2023 at 21:58
  • @user4581301 i started learning node js for this exact reason. But it's a bit slow and memory hungry on a 512mb raspberry pi. I have considered storing the data in a database. But still, if anyone would give me an example of how to start a background thread/task on a server from one client that runs even after the server ends the session and the client can later interact with the task that'd be great. Commented Mar 3, 2023 at 6:04
  • You can make it simple like : youtube.com/watch?v=iXxHtwPrQYE Or use the WebToolkit : webtoolkit.eu/wt/documentation Commented Jun 8, 2023 at 11:30

1 Answer 1

-1

This is perfectly doable with Wt; see the Server Push example.

In it simplest form, you can just start a new background thread as follows:

std::thread([](){
  ... // your background task.
}).detach();

Have a look at the Server Push example, if you want some interaction between the worker thread and your GUI.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.