Re: [SQLObject] Thread Safety and SQLObject
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: Brad B. <br...@bb...> - 2003-05-23 01:58:49
|
On 05/22/03 16:49, Luke Opperman wrote:
>
> > 1. Why isn't connection = conn making this Do The Right Thing? I
> > thought the semantics I've shown above would be creating a new DB
> > connection every time, and thus hopefully have the child avoid
> > interfering with the parent.
>
> I'm going to make a wild guess, and noting that CVS is still missing certain
> fixes that were pointed out when I last posted MemoryTransaction...:
>
> around line 683, in new(), add the following line:
> if kw.has_key('connection'):
> inst = cls(None, connection=kw['connection'])
> + inst._connection = kw['connection']
> del kw['connection']
>
> There are related bugs in RelatedJoin.add and RelatedJoin.remove, where they
> should use the connection from inst and not self.callingClass.
>
> > 2. Is there any way to use SQLObject safely in a threaded environment
> > like the os.fork'ing example I have above?
>
> So long as you don't have any of the potential concurrency problems of trying
> to update the same object from separate threads etc, I think the connection
> problem above will solve it. And even in those cases, SQLObject shouldn't blow
> up, you just don't know what data you'll end up with. :)
Well, I tried your solution but it didn't fix my problem. This isn't
because there was anything wrong with your solution, but more because
I found other problems in my own code (e.g. I hadn't quite covered
passing connections to *all* of my SQLObject-based classes being hit
in the child process :/). For simplicty's sake, I'm doing this now:
pid = os.fork()
if not pid:
# child process -- process the batch file whilst control
# has been returned to the browser
try:
conn = PostgresConnection(
host='localhost',
db='foo',
user='bar',
passwd='baz'
)
MerchantAccount._connection = conn
MerchantProxyAccount._connection = conn
MerchantUser._connection = conn
PurchaseTransaction._connection = conn
try:
batch_file.process()
except TooManyErrors, err:
pass
finally:
os._exit(0)
This also fixed another bug where the child would never return in my
Webware servlet (three cheers for os._exit()! :).
Thanks for helping me see what I was doing wrong. :)
--
Brad Bollenbach
BBnet.ca
|