[mod_python] RuntimeError: dictionary changed size during iteration

Jim Gallacher jg.lists at sympatico.ca
Fri May 27 11:25:33 EDT 2005


Hi Shack

Could you create an issue for this in JIRA?

http://issues.apache.org/jira/browse/MODPYTHON

Regards,
Jim


Shack Toms wrote:
> The mem_cleanup routine, in Session.py, appears to have a bug that causes
> python to throw "dictionary changed size during iteration".
> 
> The current code is.
> 
> def mem_cleanup(sdict):
>     for sid in sdict:
>         dict = sdict[sid]
>         if (time.time() - dict["_accessed"]) > dict["_timeout"]:
>             del sdict[sid]
> 
> The for statement should be changed to
>     for sid in sdict.keys():
> 
> This will first generate a list of the keys of the sdict, and will avoid the
> complaint about the dictionary changing on the "del sdict[sid]" statement.
> 
> The dbm_cleanup code has another approach, to use one iteration to gather
> the keys to be deleted, and then have a second iteration over the gathered
> keys which does the del.   Using that approach, mem_cleanup should become...
> 
> def mem_cleanup(sdict):
>     old = []
>     for sid in sdict:
>         dict = sdict[sid]
>         if (time.time() - dict["_accessed"]) > dict["_timeout"]:
>             old.append(sid)
>     for sid in old:
>         del sdict[sid]
> 
> This is more efficient if the number of sessions is large compared to the
> number of sessions to be deleted.
> 
> Shack Toms
> LiveData, Inc.
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 



More information about the Mod_python mailing list