race-condition

why there is no race condition using global python variable

why there is no race condition using global python variable Question: I have the following code: from threading import Thread, Lock value = 0 def adder(amount, repeats): global value for _ in range(repeats): value += amount def subtractor(amount, repeats): global value for _ in range(repeats): value -= amount def main(): value = 0 adder_thread = …

Total answers: 2

Why does Python threading.Condition() notify() require a lock?

Why does Python threading.Condition() notify() require a lock? Question: My question refers specifically to why it was designed that way, due to the unnecessary performance implication. When thread T1 has this code: cv.acquire() cv.wait() cv.release() and thread T2 has this code: cv.acquire() cv.notify() # requires that lock be held cv.release() what happens is that T1 …

Total answers: 6

Can you race condition in Python while there is a GIL?

Can you race condition in Python while there is a GIL? Question: My understanding is that due to the Global Interpreter Lock (GIL) in cPython, only one thread can ever be executed at any one time. Does this or does this not automatically protected against race conditions, such as the lost update problem? Asked By: …

Total answers: 1

How can I reproduce the race conditions in this python code reliably?

How can I reproduce the race conditions in this python code reliably? Question: Context I recently posted a timer class for review on Code Review. I’d had a gut feeling there were concurrency bugs as I’d once seen 1 unit test fail, but was unable to reproduce the failure. Hence my post to code review. …

Total answers: 4

How does using the try statement avoid a race condition?

How does using the try statement avoid a race condition? Question: When determining whether or not a file exists, how does using the try statement avoid a “race condition”? I’m asking because a highly upvoted answer (update: it was deleted) seems to imply that using os.path.exists() creates an opportunity that would not exist otherwise. The …

Total answers: 3

Atomic increment of a counter in django

Atomic increment of a counter in django Question: I’m trying to atomically increment a simple counter in Django. My code looks like this: from models import Counter from django.db import transaction @transaction.commit_on_success def increment_counter(name): counter = Counter.objects.get_or_create(name = name)[0] counter.count += 1 counter.save() If I understand Django correctly, this should wrap the function in a …

Total answers: 6

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