There is a certain page on my website where I want to prevent the same user from visiting it twice in a row. To prevent this, I plan to create a Lock object (from Python's threading library). However, I would need to store that across sessions. Is there anything I should watch out for when trying to store a Lock object in a session (specifically a Beaker session)?
-
What do you mean exactly by 'visiting twice in a row'? – wump May 08 '10 at 16:46
-
The page the user visits spawns a child process. If two of these processes are launched at once, they can crash each other. If the user were to visit the page quickly, 2 or more times, then 2 or more of these processes could be launched and bring each other down. – Jack Edmonds May 08 '10 at 17:00
2 Answers
Storing a threading.Lock instance in a session (or anywhere else that needs serialization) is a terrible idea, and presumably you'll get an exception if you try to (since such an object cannot be serialized, e.g., it cannot be pickle
d). A traditional approach for cooperative serialization of processes relies on file locking (on "artificial" files e.g. in a directory such as /tmp/locks/<username>
if you want the locking to be per-user, as you indicate). I believe the wikipedia entry does a good job of describing the general area; if you tell us what OS you're running order, we might suggest something more specific (unfortunately I do not believe there is a cross-platform solution for this).

- 854,459
- 170
- 1,222
- 1,395
-
I also realized that the Pylons/Beaker session itself has support for locking. – Jack Edmonds May 09 '10 at 15:00
I just realized that this was a terrible question since locking a lock and saving it to the session takes two steps thus defeating the purpose of the lock's atomic actions.

- 31,931
- 18
- 65
- 77