{"id":16318202,"url":"https://github.com/githubjakob/flask-gunicorn-multithreading-demo","last_synced_at":"2025-05-13T17:16:23.245Z","repository":{"id":77570439,"uuid":"515343422","full_name":"githubjakob/flask-gunicorn-multithreading-demo","owner":"githubjakob","description":"Demo Flask Application to demonstrate multithreading behavior with Gunicorn","archived":false,"fork":false,"pushed_at":"2022-07-20T09:32:43.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-16T20:28:22.469Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/githubjakob.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-18T21:15:01.000Z","updated_at":"2022-07-18T21:17:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"d98c9f95-ac98-4dc8-b354-3b74da4547bd","html_url":"https://github.com/githubjakob/flask-gunicorn-multithreading-demo","commit_stats":{"total_commits":2,"total_committers":1,"mean_commits":2.0,"dds":0.0,"last_synced_commit":"bf9c01ba17e1c956e90970430f29cf842304a881"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/githubjakob%2Fflask-gunicorn-multithreading-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/githubjakob%2Fflask-gunicorn-multithreading-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/githubjakob%2Fflask-gunicorn-multithreading-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/githubjakob%2Fflask-gunicorn-multithreading-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/githubjakob","download_url":"https://codeload.github.com/githubjakob/flask-gunicorn-multithreading-demo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990503,"owners_count":21995776,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T22:10:16.803Z","updated_at":"2025-05-13T17:16:23.170Z","avatar_url":"https://github.com/githubjakob.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask Multithreading Demo\n\nDemo Flask Application to demonstrate multithreading behavior with Gunicorn\n\n\n# Activate Virtual Env\npyenv activate flask\n\n\n# Run With Flask\ncd src \nflask run\n\n- only app.py needed\n- \"WARNING: This is a development server. Do not use it in a production deployment\"\n\n\n# Flask Debugger mode\n\nFLASK_ENV=development flask run\n\n\n# Run with Main\n\n- View Threads in Debugger in Intellij\n- View Threads in System Monitor\n\n\n# Run without Threads but Processes \n\n- See MainThreads in Debugger\n- See Processes in htop\n-- Ctrl S to freeze the View\n-- F2 -\u003e Tree View\n-- Search for Flask\n\n\n# Run with Gunicorn\n\ngunicorn --bind 0.0.0.0:5000 app:application\n\n\n# Only 1 worker = process\ngunicorn --workers 1 --bind 0.0.0.0:5000 app:application\n\n- 1 process\n- no parallel execution (/sleep)\n\n\n# Multiple threads\ngunicorn --threads 2 --bind 0.0.0.0:5000 app:application\n\n- not threadsafe /not_threadsafe\n- 2 threads generate -\u003e 2 random numbers\n\ngunicorn --threads 3 --bind 0.0.0.0:5000 app:application\n- etc\n\n\n# 2 workers = processes\ngunicorn --workers 2 --bind 0.0.0.0:5000 app:application\n\n- 2 processes\n- no multithreading problem /not_threadsafe\n\n\n# Thread local variable\n\n- allows to store values in a threadsafe variable\n\n\n# Summary\n\n- Flask has a build-in webserver which is used for development mode\n- Flask the build-in webserver supports multithreading\n- In production, we don't use flask as a webserver but Gunicorn on top of (Elastic Beanstalk)\n- Gunicorn can run with multiple workers (=processes) and multiple threads / worker (=threads)\n- OS sees and manages the threads of the python process (no \"green threads\")\n- Processes have their own space of memory, threads share the memory\n- Threads can access shared global variables, Processes can not\n- We can use threadlocal variables to store values in a threadsafe way","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithubjakob%2Fflask-gunicorn-multithreading-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgithubjakob%2Fflask-gunicorn-multithreading-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithubjakob%2Fflask-gunicorn-multithreading-demo/lists"}