[Grok-dev] Re: Initialization/Background Code? Search the grok-dev list?

Philipp von Weitershausen philipp at weitershausen.de
Sat Jun 7 17:11:42 EDT 2008

Kenneth Miller wrote:
>     Is there a quick way to search the grok-dev mailing list? It's a bit 
> hard to open it up by month and check out the subjects..

Use Google and the "site:mail.zope.org" modifier.

>     Is there anywhere to put initialization code into an app or view? 
> I've got a bit of code that parses some local files under the static 
> directory, and I'm not sure where to place the code to do this. The data 
> needs to be available to a particular view and it's associated viewlets, 
> and *should* be updated whenever the view is called.

Views have an update() method for doing things that should happen before 
the view is rendered (by a template).

What is it that you're putting into the 'static' directory, though? It's 
only meant for application-specific resources, such as images, CSS 
files, JavaScripts, etc.

>     How can I create a new thread for constant background updates?

By using Python's thread or threading module.

> I want to access information from the PyPI, but the rate at which I need 
> information is too fast to call the PyPIs XML-RPC methods in order to 
> retrieve the information. My solution was to keep a local "copy" of the 
> PyPI index for quick access, but I need a thread or something that 
> updates that local copy at a regular interval (every hour or so) and 
> doesn't depend on a View to be called.

Sounds reasonable. You can even open a connection to the database from 
that thread and modify the ZODB (which is, I think, what you want to do 
in order to store the data):

   import transaction
   from zope.component import getUtility
   from ZODB.interfaces import IDatabase

   db = getUtility(IDatabase)
   conn = db.open()
   root = db.root()['Application']
   # do modifications here to root[...]

Just remember to keep the connection (and more importantly, the 
transaction) short-lived so that you won't risk conflict errors with 
concurring threads.

More information about the Grok-dev mailing list