Sylvain Hellegouarch <
s...@defuze.org> writes:
> On 17 April 2014 22:20, Bao Niu <
niub...@gmail.com> wrote:
>
>>
>> Specifically, regarding the architecture of the code, why does it take two
>> modules, cherrypy.Tools and cherrypy.process.plugins to fulfil a single
>> task -- attaching a call back to the bus? What different purpose do Tools
>> and plugins serve respectively?
>>
>> Maybe my question is too junior, I really appreciate a little hint because
>> currently I feel completely lost in this code. Thanks.
>>
>>
> They are not compulsory but Tools and Plugins will make your life much
> easier once you grasp them. Basically, a tool is mostly a function that
> runs during the request/response handling at a given stage of the request
> processing's life. It's similar, in psir to Django or WSGI middleware,
> though the design is different.
>
> Plugins are standalone piece of software that are associated with the
> application server's life. They aren't even aware of the HTTP stack. They
> are just functionalities that are available across the whole stack for you
> to call, whenever you need.
>
One thing that helped ease the confusion of the bus and plugins
vs. tools was at what scope do they work within a cherrypy app.
As Sylvain pointed out, if you want to hook into the request / response
cycle then use a Tool. Tools are applied for each request can't keep any
state beyond the request / response.
Plugins, on the other hand, are at the process level. These can manage
state across requests. If you wanted to start some service or create a
pool of workers that stop when the cherrypy server exits, then you want
to use a Plugin.
Going back to SQLAlchemy, assuming I remember the example correctly, the
plugin creates the session and a pool of connections that can be
used. The tool then asks the session for a connection for the life of
the request. This seperation of scope is nice because you have a clear
distinction how you write code. If you are doing something per-request,
use a Tool. If you want to something that lives past a request (like
running other processes), then use a plugin.
Just to give a final example of how we've used plugins, we have test
suite that starts up a cherrypy app that requires a suite of backing
services. Rather than use our test suite to manage this, we have some
plugins that start the supporting services and shut them down when the
tests have finished.
When we needed to use a specialized auth token for our cherrypy apps, we
wrote a tool. The tool could be applied to the URLs we required the
token for and could verify the token on each request.
These kind of examples are what made plugins and tools really make
sense for me. In general, you'll probably have fewer needs for plugins than
tools, but if your application does require a more complex backend,
plugins can be a helpful way to manage your process level requirements.
Best of luck!
Eric
--
Sent with my mu4e