vlad_pion's picture

Hi, wanted to ask if
it is 'within-the-design-objectives' to use pion as multithreaded report server

In this scenario
a)
a user submits a request (which is an entry in a message table in postgresql database) -- asking to generate a report.
b) an instance of pion server that 'monitors' that table scans the table, finds the message, updates it to 'being processed'

c) sends an http request (using curl) to another instance of pion server. that one is running on a more powerfull machine. It gets the request, says asynch 'thank you', unlocks one of the 'worker threads' that also have a precreated ODBC database connection. The worker works on the report -- for about 4 hours, then updates the message record saying -- done.

my questions sofar are:
1) can pion's internal schedule be used to 'setup' poll interval for the poller process (in item b)
2) is it ok for a worker thread to do work that takes a long time and whose result is not an http response.
3) is there an example I could use that would explain to me how to 'precreate' a db connectio per thread (or may be a have a pool of db connections (which would be overkill))

thank you in advance,
Vlad

Mike Dickey's picture
Using PionScheduler

Hi Vlad,

(I know I already responded via the mailing list, but just wanted to duplicate it here in case anyone else might have the same question...)

I think you can use a PionScheduler class to handle the async work. It's just a wrapper around the Boost.asio code that also starts and manages a thread pool for you.

The easiest route would probably be to just re-use the scheduler that is used by TCPServer/HTTPServer (the downside would be that you're sharing threads with the web server). Something along the following lines:


class MyHttpInterface
: public pion::net::HTTPServer
{
public:
 
virtual ~MyHttpInterface() {}
 
MyHttpInterface() {
// bind "/report" HTTP resource to the handleReportRequest function
HTTPServer::addResource("/report", boost::bind(&MyHttpInterface:: handleReportRequest, this, _1, _2));
...
}
 
private:
 
void generateReport(void) { ... }
 
void handleReportRequest(HTTPRequestPtr&, TCPConnectionPtr&) {
// post asynchronous work to generate a new report (returns immediately)
  TCPServer::getIOService().post(boost::bind(&MyHttpInterface::generateReport, this));
 
// send ok response
...
}
};

Take care,
-Mike

Submitted by Mike Dickey on Tue, 02/10/2009 - 17:02.