locking

Locking in sqlalchemy

Locking in sqlalchemy Question: I’m confused about how to concurrently modify a table from several different processes. I’ve tried using Query.with_lockmode(), but it doesn’t seem to be doing what I expect it to do, which would be to prevent two processes from simultaneously querying the same rows. Here’s what I’ve tried: import time from sqlalchemy.orm …

Total answers: 2

Race conditions in django

Race conditions in django Question: Here is a simple example of a django view with a potential race condition: # myapp/views.py from django.contrib.auth.models import User from my_libs import calculate_points def add_points(request): user = request.user user.points += calculate_points(user) user.save() The race condition should be fairly obvious: A user can make this request twice, and the application …

Total answers: 6

Make sure only a single instance of a program is running

Make sure only a single instance of a program is running Question: Is there a Pythonic way to have only one instance of a program running? The only reasonable solution I’ve come up with is trying to run it as a server on some port, then second program trying to bind to same port – …

Total answers: 23

What is the best way to open a file for exclusive access in Python?

What is the best way to open a file for exclusive access in Python? Question: What is the most elegant way to solve this: open a file for reading, but only if it is not already opened for writing open a file for writing, but only if it is not already opened for reading or …

Total answers: 7

Are locks unnecessary in multi-threaded Python code because of the GIL?

Are locks unnecessary in multi-threaded Python code because of the GIL? Question: If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all? If the GIL doesn’t allow multiple instructions to be executed in parallel, wouldn’t shared data …

Total answers: 9