<span>> > However, the thread has to start a new transaction each time it</span><br><span>> </span> <span>> processes something - which I know how to do:</span><br><span>> </span> <span>></span><br>
<span>> </span> <span>> while True:</span><br><span>> </span> <span>> #wait until asked to do something</span><br><span>> </span> <span>> import transaction</span><br>
<span>> </span> <span>> transaction.begin()</span><br><span>> </span> <span>></span><br><span>> </span> <span>> However, the thread needs access to the root object in order to turn</span><br>
<span>> </span> <span>> the identifiers gotten from the requests into persistent objects...</span><br><span>> </span> <span>> how would I go about accessing the root object in such a circumstance?</span><br>
<span>> </span> <br><span>> </span> <span>You need to pass the database object to the thread, and the thread needs</span><br><span>> </span> <span>to open a connection (connection = db.open()). Then connection.root()</span><br>
<span>> </span> <span>will give you the root object (or you could pass OIDs to the thread and</span><br><span>> </span> <span>use connection.get(oid) to find the objects you need to work with).</span><div><br></div>
<div>That makes sense. I wasn't sure where to get a db object from so here's what</div><div>I ended up doing. Let me know if there's a better way.</div><div><br></div><div>I use paster to run the webserver, so in run.py I now have:</div>
<div><br></div><div> GLOBAL_CONFIG = [None]</div><div><div> def app(global_config, **settings):</div><div> GLOBAL_CONFIG[0] = global_config</div><div> #...</div><div><br></div><div>Now in the thread I start to do the processing I have:</div>
<div><br></div><div><div> def proc_f(self, i):</div><div> from repoze.zodbconn.uri import db_from_uri</div><div> import run</div><div> global_config = run.GLOBAL_CONFIG[0]</div><div> uri = global_config['zodb_uri'] </div>
<div> db = db_from_uri(uri)</div><div> </div><div> conn = db.open()</div><div> app_root = conn.root()['app_root']</div></div><div><br></div>
while True:</div><div> #wait for request...</div><div><br></div><div> t = conn.transaction_manager.begin()</div><div> #do a bunch of read-only processing, queue results</div><div>
conn.abort(t) </div><div><br></div>> Don't forget to commit or abort the transaction, and also don't forget<br>> that you may need to implement some kind of retry logic if commit()<br>> raises a ConflictError due to conflicting updates.<br>
<br><div>I figure since I never write anything to the database in my worker thread I can</div><div>always abort the transaction. </div><div><br></div><div>This all seems to work. Anything horribly wrong I'm doing? </div>
<div><br></div><div>Thanks muchly,</div><div><div>- Claudiu
</div>
</div>