python / python/cpython

Thread-safe atomic counters

Open
#124,366 3 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-free-threading type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Python doesn't currently have a thread-safe atomic counter. CPython currently internally uses a few different patterns to implement thread-safe counters. For example, some test code uses appends to a shared list because the implementation of list.append() is atomic.

We should provide a counter that is explicitly thread-safe so that people don't have to rely on other idioms. The minimum initial API should provide:

  • atomic increment
  • get value (i.e., inspect)

The threading module may be a good place for it.

cc @rhettinger

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the threading module and the existing thread-safe counter patterns described in the issue. Define and test the minimum API for atomic increment and value inspection, including behavior under concurrent access.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.