Python을 이용한 웹 서비스에서 동시 요청의 수가 증가하고 이를 처리하는 것이 중요하다면 WSGI 표준(동기식 웹 프레임워크)을 포기하고 tornado나 twisted 같은 비동기 프레임워크를 사용하는 것도 좋습니다.
https://twistedmatrix.com/trac/
Twisted
Twisted is an event-driven networking engine written in Python and licensed under the open source MIT license. It supports CPython 3.5+ and PyPy3. Twisted makes it easy to implement custom network applications. Here's a TCP server that echoes back every
twistedmatrix.com
https://www.tornadoweb.org/en/stable/
Tornado Web Server — Tornado 6.1 documentation
Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and othe
www.tornadoweb.org
다음 예시는 twisted를 기반으로 8080번 포트에서 현재 시간을 json 형태로 반환하는 웹 서버 입니다.
import time
from twisted.web import server, resource
from twisted.internet import reactor, endpoints
class Simple(resource.Resource):
isLeaf = True
def render_GET(self, request):
request.responseHeaders.addRawHeader(b"content-type", b"application/json")
retgurn bytes(json.dumps({'time' : time.time()}), 'utp8')
site = server.Site(Simple())
endpoint = endpoints.TCP4ServerEndpoint(reactor,8080)
endpoint.listen(site)
reactor.run()
twisted는 다양한 기능을 제공하는(SSH, mail 등..) 효과적인 프레임워크지만 HTTP 기반 마이크로서비스로 사용하는 경우 다음과 같은 문제가 있습니다.
- Resource 클래스를 상속받는 클래스로 각 마이크로서비스의 endpoint를 구현해야하고, 필요한 함수도 구현해야 합니다. 단순한 API를 만들기 위해 많은 코드가 추가됩니다.
- 비동기적인 성질 때문에 이해하기가 어렵고, 디버그가 힘듭니다.
- 트리거를 연달아 발생시키기 때문에 콜백 지옥에 빠지기 쉽습니다.
- twisted의 어플리케이션은 테스트가 힘듭니다. twisted에 종속된 단위 테스트 모델을 사용해야 합니다.
마이크로서비스에서 twisted 프레임워크의 문제점을 알아보았습니다.
참조
https://twistedmatrix.com/trac/
Twisted
Twisted is an event-driven networking engine written in Python and licensed under the open source MIT license. It supports CPython 3.5+ and PyPy3. Twisted makes it easy to implement custom network applications. Here's a TCP server that echoes back every
twistedmatrix.com
파이썬 마이크로서비스 - 교보문고
모범 사례로 알아보는 파이썬 마이크로서비스 개발, 테스트,배포, 확장까지 | ★ 이 책에서 다루는 내용 ★■ 마이크로서비스 이해와 설계 방법■ 파이썬 3, 플라스크, Tox, 다른 도구와 모범 사례
www.kyobobook.co.kr
'About > Flask' 카테고리의 다른 글
[Python] Flask url_for 함수 (0) | 2021.09.05 |
---|---|
[Python] Flask 변수와 컨버터 (0) | 2021.09.05 |
[Python] boto3를 이용한 Flask-S3 연동 (파일 업로드 및 다운로드) (1) | 2021.07.19 |
[Python] Flask를 이용한 요청 처리 (0) | 2021.06.07 |
[Python] Greenlet과 Gevent(WSGI 성능 향상) (3) | 2021.06.06 |