Python/Python 뜯어보기

[Python 뜯어보기] 4. WSGI 와 Python

동현 유 2022. 3. 22. 17:05

1. WSGI 란?

"웹 서버 게이트웨이 인터페이스"라는 의미로, WS 와 WAS 사이의 인터페이스를 제공한다.

    (1) WS 를 변경해야할 일이 있거나 추가해야 할 때,

    (2) 또는 반대로 다른종류의 WAS 를 추가해야하거나 변경해야할 때

등의 경우에 ws 와 was 사이의 추가적인 설정을 하지 않아도 손쉽게 갈아낄울 수 있도록 돕는듯하다.

(추상화 수준이 높은 것을 의존한다는 제어의 역전 개념과 비슷한듯) 

(제어의 역전 개념은 이전 포스팅(https://letsmakemyselfprogrammer.tistory.com/63) 참고)

 

2. UWSGI

  - wsgi 의 수많은 종류 중 하나. (gunicorn, Bjoern, CherryPy 등).

  - 다양한 언어 지원

  -  unix 소켓을 지원

 

3. WSGI 가 왜 필요한데..?

  개인적인 생각.

python GIL 때문에 다른 언어에 비해 동시접속 처리 능력이 많이 떨어진다. (실험해본 사람이 있네)

=> 보완하기 위한 해결책이 아닐까?

python was 를 여러 프로세스로 관리하고 스레드도 관리해주는.. 일을 한다.

 

https://www.reddit.com/r/Python/comments/4s40ge/understanding_uwsgi_threads_processes_and_gil/ 를 참고하자면

uWSGI works by creating an instance of the python interpreter and importing the python files related to your application. If you configure it with more than one process, it will fork that instance of the interpreter until there are the required number of processes. This is roughly equivalent to just starting that number of python interpreter instances by hand, except that uWSGI will handle incoming HTTP requests and forward them to your application. This also means that each process has memory isolation—no state is shared, so 
each process gets its own GIL.

즉 python WAS 를 여러대 띄워서 처리 능력을 향상시키는 것인데,

일일이 수작업으로 띄우면서 관리하기가 힘드니까, uwsgi 가 그 일을 해주는 것이다.

 

 

4. uWSGI 의 프로세스 관리 

https://uwsgi-docs.readthedocs.io/en/latest/Cheaper.html 에 따르면

 

default 로 1개의 프로세스가 대기하고 있고,

 

요청이 감당할 수 없으면 (임계치를 감지하는 알고리즘도 설정할 수 있다.) default 로 1개씩 더 프로세스가 생성된다.

 

최대 default 로 50개까지 생성된다.

 

그렇다가 프로세스가 idle 한 상태가 되면 해당 프로세스를 종료시킨다.

 

 

5. WSGI 에 대한 PEP-3333 가이드

 

 

PEP 3333 – Python Web Server Gateway Interface v1.0.1 | peps.python.org

The second parameter passed to the application object is a callable of the form start_response(status, response_headers, exc_info=None). (As with all WSGI callables, the arguments must be supplied positionally, not by keyword.) The start_response callable

peps.python.org