r/Python 16h ago

Discussion When do you prefer asyncio.Semaphore over an asyncio.Queue for limiting concurrency?

37 Upvotes

I've been thinking about concurrency control in asyncio.

A common pattern for limiting concurrent work is:

sem = asyncio.Semaphore(10)

async with sem:
    await do_work()

But in many cases, couldn't the same problem be modeled by putting work into an asyncio.Queue and running a fixed number of worker tasks?

I'm curious how experienced Python developers decide between the two approaches.

Are there real-world situations where a semaphore is clearly the better abstraction than a worker queue? Are there meaningful differences in cancellation behavior, backpressure, fairness, task lifetime, or code complexity?

I'd especially be interested in examples from production async Python code.


r/Python 4h ago

Resource Python Under the Hood Update: Chapter 3 (Conditional Statements & Control Flow) is now complete

5 Upvotes

Hey everyone,

Chapter 3 is finally done. This chapter took way more time than I originally expected because it grew into a much deeper and larger chapter than planned.

Current Progress:
✅ Chapter 1 - Variables & Memory
✅ Chapter 2 - Expressions & Operators
✅ Chapter 3 - Conditional Statements & Control Flow

I'd love to hear your thoughts on the new chapter and any suggestions for future improvements.

With this, I’m taking a break from Python Under the Hood. I’ll be stepping away from the project for a while and will return to it before July 2027.

When I come back, we’ll continue with even deeper explanations, more advanced topics, and an even better approach to understanding what happens under the hood of Python.

For now, Chapter 3 marks the end of this phase. 🐍⚙️

GitHub: python-under-the-hood

See you in the next chapter.


r/Python 22h ago

Daily Thread Tuesday Daily Thread: Advanced questions

3 Upvotes

Weekly Wednesday Thread: Advanced Questions 🐍

Dive deep into Python with our Advanced Questions thread! This space is reserved for questions about more advanced Python topics, frameworks, and best practices.

How it Works:

  1. Ask Away: Post your advanced Python questions here.
  2. Expert Insights: Get answers from experienced developers.
  3. Resource Pool: Share or discover tutorials, articles, and tips.

Guidelines:

  • This thread is for advanced questions only. Beginner questions are welcome in our Daily Beginner Thread every Thursday.
  • Questions that are not advanced may be removed and redirected to the appropriate thread.

Recommended Resources:

Example Questions:

  1. How can you implement a custom memory allocator in Python?
  2. What are the best practices for optimizing Cython code for heavy numerical computations?
  3. How do you set up a multi-threaded architecture using Python's Global Interpreter Lock (GIL)?
  4. Can you explain the intricacies of metaclasses and how they influence object-oriented design in Python?
  5. How would you go about implementing a distributed task queue using Celery and RabbitMQ?
  6. What are some advanced use-cases for Python's decorators?
  7. How can you achieve real-time data streaming in Python with WebSockets?
  8. What are the performance implications of using native Python data structures vs NumPy arrays for large-scale data?
  9. Best practices for securing a Flask (or similar) REST API with OAuth 2.0?
  10. What are the best practices for using Python in a microservices architecture? (..and more generally, should I even use microservices?)

Let's deepen our Python knowledge together. Happy coding! 🌟


r/Python 5h ago

News DeployProof 1.1.11: Fast diff-scoped mutation testing and pre-push quality gate for Python

0 Upvotes

GitHub: https://github.com/SVSPraveen/DeployProof

PyPI: https://pypi.org/project/deployproof/

Hey everyone,

We’ve all encountered test suites boasting 90%+ or even 100% line coverage where the tests execute every line of code but assert almost nothing. With the rise of AI-assisted coding where copilots generate boilerplate tests that execute functions without asserting true invariants, line coverage has become a dangerously hollow metric.

The gold standard for proving that tests actually assert correctness is **mutation testing** (modifying AST operators like `==` to `!=`, `<` to `>`, or returning `None` to see if tests fail). But traditional mutation testing is painfully slow.

---

###What My Project Does

DeployProof is a deterministic, 100% local pre-push verification gate that solves the mutation latency problem by dropping feedback loops down to 2 to 5 seconds.

Instead of mutating whole files or the entire repository, DeployProof parses your active `git diff` against your base branch (or uncommitted working tree), maps changed line spans to their specific AST subtrees, and generates isolated mutations **strictly on newly written or touched logic**.

In addition to diff-scoped mutation testing, DeployProof provides **5 automated hygiene gates** that standard linters routinely miss:

  1. Diff-Scoped AST Mutation Engine (2–5s): Generates arithmetic, comparison, boolean, and boundary mutations strictly on changed AST nodes. If tests don't fail when logic is inverted, the gate blocks push with the exact surviving mutant line.
  2. Dependency Hallucination & Slopsquatting Defense: When new packages are introduced in `requirements.txt` or inline imports, DeployProof queries the public PyPI JSON API before push to verify the package actually exists on the public registry (guarding against hallucinated packages, typosquatting, and brand-new suspicious releases).
  3. Entropy Secret & Credential Scanning: Uses Shannon entropy analysis and regex pattern matching to detect hardcoded API keys (OpenAI, AWS, Anthropic, tokens) and tracked `.env` files in session diffs.
  4. Control Flow & Error Handling Hygiene: Detects and flags blanket `except Exception: pass` anti-patterns, swallowed exceptions, and unverified mock/monkeypatch fixtures introduced into tests.
  5. GhostApproval Symlink Defense: Traps repository sandbox-escaping symlinks before commits.

###Deep Multi-Worker Audits (`--full-repo --workers 8`)

For full-codebase audits, DeployProof includes a built-in parallel execution engine (`ProcessPoolExecutor`). It initializes isolated, PID-keyed filesystem sandboxes (`worker_<PID>`) with dedicated `--override-ini=cache_dir=...` and separate `--basetemp=...` pytest roots to scale across CPU cores without state leaks, cache collisions, or lock contention.

#### Zero Telemetry & 100% Local

DeployProof runs completely offline on your machine. The only outbound network call is checking package existence against the public PyPI registry. Zero code, findings, or telemetry ever leave your machine.

---

### Target Audience

DeployProof is built for:

Python Developers & Engineering Teams: Who want an automated, deterministic local gate before opening pull requests to ensure new code actually has assertion backing.

AI-Assisted Workflows: Developers using Cursor, Claude, Copilot, or ChatGPT who need a fast sanity gate to catch hallucinated package imports, unverified test mocks, and hollow line-coverage tests before pushing.

CI/CD Pipeline Maintainers: Teams looking for structured machine-readable JSON output (`deployproof check --json`) to enforce mutation thresholds in GitHub Actions without waiting 45 minutes.

---

### Comparison to Existing Tools

vs. pytest-cov / Line Coverage: Line coverage measures execution paths, not assertion quality. A test that calls a function without an `assert` gives 100% line coverage. DeployProof inverts logic operators (`==` to `!=`, `<` to `>`) to prove your tests actually fail when a bug is introduced.

vs. mutmut: Mutmut is the standard for full-repo mutation testing, but running it across an entire codebase takes 20 to 60+ minutes sequentially. DeployProof is designed specifically as a pre-push hook: it scopes mutations to the git diff in 2–5 seconds, and provides isolated parallel multi-worker sandboxes when running full audits.

vs. bandit / trufflehog: Linters like Bandit analyze static security patterns, and Trufflehog searches for git history secrets. DeployProof combines diff-scoped credential entropy with live PyPI package existence verification and AST mutation testing into a single sub-5-second pre-push command.

---

### Quick Start

```bash

# Install via pip or pipx

pip install deployproof

# or

pipx install deployproof

# Run check on your active changes / git diff:

deployproof check

# Run a parallel full-repository audit:

deployproof check --full-repo --workers 8

# Output machine-readable JSON for CI/CD:

deployproof check --json

```


r/Python 15h ago

Discussion I know I’m late, but Python 3.14.7 is out. Anyone actually upgraded yet?

0 Upvotes

I know I’m a little late to this, but I’ve been looking into Python 3.14.7 and something keeps bothering me.

The release itself looks solid. Python 3.14.7 was released on August 5 and is mainly a maintenance release with a lot of bug fixes and improvements.

But for people working on existing projects, the bigger question seems to be:

What makes you decide that it’s finally time to upgrade Python?

A new Python version sounds great until you have to check:

  • dependencies
  • C extensions
  • Docker images
  • CI/CD
  • production servers
  • test suites
  • old packages that nobody wants to touch

And Python 3.14 has some pretty interesting changes compared with 3.13, including officially supported free-threaded Python, t-strings, multiple interpreters, and the new compression.zstd module.

So I’m curious about real-world experience rather than release notes.

Have you upgraded to 3.14 yet?

If yes, what went smoothly and what broke?

If no, what’s the main reason you’re waiting?