Re: [Cppcms-users] Mounting both synchronous and asynchronous applications
Brought to you by:
artyom-beilis
|
From: Artyom <art...@ya...> - 2011-02-01 06:08:36
|
> From: CN <cn...@gr...>
> Hello,
>
> Imagine my site has both synchronous (for user login) and asynchronous
> (for interactive activities) pages. Is it a correct and good practice to
> mount both synchronous and asynchronous applications like the following
> code?
>
> //code begin
> class login:public cppcms::application{ .... }
> class interaction:public cppcms::application{ .... }
>
> int main()
> {
> cppcms::service s(argc,argv);
>
> s.applications_pool().mount(cppcms::applications_factory<login>());
>
> booster::intrusive_ptr<interaction> i=new interaction(s);
> s.applications_pool().mount(i);
>
> s.run();
> }
> //code end
>
Yes and not, you should provide a mount point
that would distinguish between them:
See:
-
http://art-blog.no-ip.info/cppcms_ref_v0_99/classcppcms_1_1applications__pool.html
- http://art-blog.no-ip.info/cppcms_ref_v0_99/classcppcms_1_1mount__point.html
> Please also correct me if my following thoughts are wrong:
>
> - When a request arrives, cppcms::service.run() loops all mounted
> applications and pass that request to every mounted application. It is the
> responsibility of every application to filter the interested passed in
> request by applying dispatcher().assign().
>
Not it would pass the request to the first application
that matches the mount point pattern. And it would not
it to only one such application.
Note it is multi-layered search:
If there is no specific patch assigned to dispatcher
it would return error 404 (unless you had redefined
application::main and do not use dispatcher
> - This program can fire multiple instances of "login" class. Every login
> instance runs as a separate thread. Thus this program can concurrently
> process multiple incoming synchronous (login) requests.
>
Yes and No.
A pool of synchronous applications is created.
At each request once instance is taken from the
applications pool and passed to thread pool for execution.
Once it completes it is recycled for reuse in the pool.
So multiple instances run synchronously concurrently.
> - This program process asynchronous requests only one at a time because
> only one instance of class "interaction" is created.
>
No, asynchronous application is executed in main event loop,
when new request is ready it is passed to the application, unlike
the case of synchronous application it can detach the http::context
object and "keep" it for further response (to provide long polling).
and then get more requests.
So basically asynchronous application can handle multiple
request and this is needed mostly for server side events
and long polling.
See examples/chat
> As such, asynchronous
> applications are most likely the bottleneck of performance when the load
> is heavier.
If you do heavy processing there then yes, but you should not.
You are expected to do very lightweight operations there
if you have heavy processing either use synchronous application
and notify the asynchronous one passing it the results.
Or use the CppCMS's thread pool to handle heavy operations.
Don't forget that you can't access asynchronous app. directly
from other threads you rather need to use cppcms::service::post
to pass the handler to the main event loop for execution
to ensure thread safety.
Basically:
- When you need COMET/Server side events use asynchronous app,
- Otherwise use synchronous one.
Artyom
|