How to handle performance issues in Python?
Find and fix slow Python code: profile first, then improve algorithms, cache results and pick the right concurrency model for your bottleneck.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Find and fix slow Python code: profile first, then improve algorithms, cache results and pick the right concurrency model for your bottleneck.
Python frees memory automatically, but leaks still happen. Learn to find them with tracemalloc and fix caches, cycles and unclosed resources.
Catch and handle Python runtime errors with try-except, custom exceptions and logging. Includes a table of common exceptions.
A practical Python code review checklist covering correctness, tests, readability, security and the tools that automate the mechanical checks.
Set up a Python CI/CD pipeline with GitHub Actions: run tests and linting on every push, then build and deploy only after the checks pass.
Combine Bandit, pip-audit, OWASP ZAP and pytest security tests to find vulnerabilities in a Python application.
Run load tests with Locust or JMeter, then benchmark and profile slow Python code with timeit, cProfile and py-spy.
Stress test web services in Python with Locust: write user tasks, ramp load in steps, run headless in CI and read percentile latency and error rates.
Load test a web app with Locust: define user behavior in Python, run it headless in CI, read latency percentiles and scale with distributed workers.
Write functional tests in Python with pytest and unittest, from a simple module to an HTTP API and a Playwright browser test, with fixtures and best practices.
Write reliable Python integration tests with pytest: combine real components, use fixtures and an in-memory database, and mock only external APIs.
Write and run Python unit tests with unittest and pytest, including fixtures, parametrized cases, mocking external calls and coverage reports.
Keep passwords and keys out of source code, hash user passwords with Argon2id or bcrypt, and encrypt recoverable data with vetted libraries.
Use verified TLS, SSLContext, authenticated encryption and signed tokens to protect Python network traffic, and avoid removed APIs like ssl.wrap_socket.
Sign data with a private key and verify it with the public key using Ed25519, RSA-PSS or ECDSA in the cryptography library, with PEM key storage.
Learn secure encryption in Python using Fernet, scrypt key derivation, AES-GCM and RSA-OAEP, plus key storage advice and common errors.
Use CSRF tokens, SameSite cookies and side-effect-free GET requests. Django includes CSRF middleware, and Flask needs Flask-WTF's CSRFProtect.
Never build SQL from user input. Use parameter placeholders or an ORM, allow-list identifiers, and run with least-privilege database accounts.
Check a Python app for security issues with Bandit, pip-audit and secret scanners, plus safe coding patterns and a CI workflow that runs on every pull request.
Use kubectl rollout history and kubectl rollout undo to return a Python Deployment to an earlier revision, then verify it and keep Git in sync.