On Dec 18, 2008, at 8:54 AM, Harish Vishwanath wrote:

> Hello,
>
> I have a scenario like below :
>
> - Session has weak_identity_map set to true. Therefore session is  
> self pruning.
> - In some situations, the session might contain object references,  
> which have been deleted by other sessions from the database.
> - In such cases, I want to remove the references of those deleted  
> objects from this session's identity_map.
> - Running a gc.collect() could be expensive.
> - I just have primary key of the object that got deleted from  
> another session.
>
> Which is the fastest way to remove this specific object from the  
> session's identity_map? Currently I am doing something like below :
>
> # dkey is a tuple and will have the primary key of the object that  
> just got deleted from the db through another session.
>
> dobj = None
> for obj in session.identity_map:
>    c, k = obj #extract class and the primary key tuple from the  
> object.
>    if k == dkey:
>      dobj = obj #get the object reference to be deleted.
>      break  #The session wont contain multiple references to the  
> same obj anyway.
>
> session.identity_map.remove_key(dobj)  #delete that object from the  
> session.
>
> I am worried about the iteration that I do, which could consume  
> significant time in case session has a lot of objects after a bulk  
> query/insertion etc., Is there a better way to do this?

I'm not sure here what is the actual problem you're trying to  
solve ?   If your mapped objects contain no cyclical references, you  
might find that they don't spend any time whatsoever in the Session  
beyond your explicit referencing of them.   gc.collect() is only  
needed for collection of cycles.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to