<style>
.present {
text-align: left;
}
</style>
# Background Info for Python Projects
## Week 19 Day 5
---
## Python Threading
A thread is a flow of execution.
Python has multithreading capabilities, meaning it can be configured to run multiple threads at the same time, which allows for faster runtimes.
Multithreading is possible on both single-core and multi-core CPUs thanks to **concurrency**.
---
## Python Threading
Cool! However, multithreading is difficult to configure and can cause problems if threads are not scheduled correctly.
The Global Interpreter Lock (GIL) can also inhibit multithreading functionality.
---
## What is the Global Interpreter Lock (GIL)?
The GIL allows only one thread at a time to control the Python interpreter.
In other words, only one thread can be in a state of execution at a time.
---
## What is Gunicorn?
Gunicorn ('Green Unicorn') is an application server. It runs Python web server gateway interface (WSGI) applications - like Flask!
Gunicorn is a **separate process** running on a different port than your web server. Gunicorn relies on multithreading - implementing "pre-fork worker model" - meaning it spawns sub-processes as needed to do the work of passing requests and responses.
---
## Why do we need Gunicorn for our Flask projects?
Gunicorn communicates server requests to your application & application responses back to the server.
Flask only has a built-in development server. You must use Gunicorn or another WSGI server to run your app in a production environment.
---
## Connecting Flask & React
---
### Redirect React server requests to Flask
In development, redirect API requests from the react app server to the Flask API server via a proxy key in the React app's *package.json*.
```python=
"proxy": "http://localhost:5000"
```
In production, Gunicorn will redirect these requests for us!
{"metaMigratedAt":"2023-06-16T04:29:19.023Z","metaMigratedFrom":"Content","title":"Background Info for Python Projects","breaks":true,"contributors":"[{\"id\":\"a6f34c0b-3567-4ed5-ba81-c2299c2d9369\",\"add\":6110,\"del\":4202}]"}