๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Back-end & Server/FastAPI

[FastAPI] ๊ฐœ์š”

728x90
๋ฐ˜์‘ํ˜•

๐Ÿฐ FastAPI ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด์„œ ๊ฐœ์ธ์ ์œผ๋กœ ์ •๋ฆฌํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.

 

 

"FastAPI๋Š” ํ˜„๋Œ€์ ์ด๊ณ , ๋น ๋ฅด๋ฉฐ(๊ณ ์„ฑ๋Šฅ), ํŒŒ์ด์ฌ ํ‘œ์ค€ ํƒ€์ž… ํžŒํŠธ์— ๊ธฐ์ดˆํ•œ Python3.8+์˜ API๋ฅผ ๋นŒ๋“œํ•˜๊ธฐ ์œ„ํ•œ ์›น ํ”„๋ ˆ์ž„์›Œํฌ"
๋ผ๊ณ  ๊ณต์‹ ๋ฌธ์„œ์— ์“ฐ์—ฌ์žˆ๋‹ค.

 

์ฃผ์š”ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

ํŠน์ง• ๋‚ด์šฉ
๋น ๋ฆ„ (Starlette๊ณผ Pydantic ๋•๋ถ„์—NodeJS ๋ฐ Go์™€ ๋Œ€๋“ฑํ•  ์ •๋„๋กœ ๋งค์šฐ ๋†’์€ ์„ฑ๋Šฅ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ๋น ๋ฅธ ํŒŒ์ด์ฌ ํ”„๋ ˆ์ž„์›Œํฌ ์ค‘ ํ•˜๋‚˜
๋น ๋ฅธ ์ฝ”๋“œ ์ž‘์„ฑ ์•ฝ 200%์—์„œ 300%๊นŒ์ง€ ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ ์†๋„ ์ฆ๊ฐ€
์ ์€ ๋ฒ„๊ทธ ์‚ฌ๋žŒ(๊ฐœ๋ฐœ์ž)์— ์˜ํ•œ ์—๋Ÿฌ ์•ฝ 40% ๊ฐ์†Œ
์ง๊ด€์„ฑ ํ›Œ๋ฅญํ•œ ํŽธ์ง‘๊ธฐ ์ง€์›. ๋ชจ๋“  ๊ณณ์—์„œ ์ž๋™์™„์„ฑ. ์ ์€ ๋””๋ฒ„๊น… ์‹œ๊ฐ„
์‰ฌ์›€ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ๋ฐฐ์šฐ๋„๋ก ์„ค๊ณ„, ์ ์€ ๋ฌธ์„œ ์ฝ๊ธฐ ์‹œ๊ฐ„
์งง์Œ ์ฝ”๋“œ ์ค‘๋ณต ์ตœ์†Œํ™”, ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ ์–ธ์˜ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ, ์ ์€ ๋ฒ„๊ทธ
๊ฒฌ๊ณ ํ•จ ์ž๋™ ๋Œ€ํ™”ํ˜• ๋ฌธ์„œ์™€ ์ค€๋น„๋œ ํ”„๋กœ๋•์…˜ ์šฉ ์ฝ”๋“œ 
ํ‘œ์ค€ ๊ธฐ๋ฐ˜ API์— ๋Œ€ํ•œ (์™„์ „ํžˆ ํ˜ธํ™˜๋˜๋Š”) ๊ฐœ๋ฐฉํ˜• ํ‘œ์ค€ ๊ธฐ๋ฐ˜: OpenAPI (์ด์ „์— Swagger๋กœ ์•Œ๋ ค์กŒ๋˜) ๋ฐ JSON ์Šคํ‚ค๋งˆ

 

 

์„ค์น˜ 

pip install "fastapi[all]"

# pip install fastapi ๋กœ ์„ค์น˜์‹œ Uvicorn ๊ฐ™์€ ์˜์กด์„ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋”ฐ๋กœ ์„ค์น˜ํ•ด์•ผํ•จ.

 

 

Uvicorn?

CGI(Common Gateway Interface)

  • ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„์— ๋™์ ์ธ ์š”์ฒญ์ด ๋“ค์–ด ์™”์„ ๋•Œ ๊ทธ๊ฒƒ์— ๋Œ€ํ•œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‘๋‹ต์ด ํ•„์š”ํ•จ.
  • ์ด๋•Œ ์„œ๋ฒ„๋งˆ๋‹ค, ์–ธ์–ด๋งˆ๋‹ค ์ด ํ˜•ํƒœ๊ฐ€ ๋‹ค๋ฅด๋ฉด ๊ณค๋ž€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณตํ†ต ๊ทœ์•ฝ(Interface)๋ฅผ ๋งŒ๋“ค์–ด์•ผํ•จ.
  • ์ด Interface๋ฅผ CGI(Common Gateway Interface)

 

WSGI(Web Server Gateway Interface)

  • CGI์˜ ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ ๋•Œ ์ƒˆ๋กœ์šด ํ”„๋กœ์„ธ์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋“ฑ์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•œ ๋ฐฉ๋ฒ•.
  • Callback Object ๋“ฑ์œผ๋กœ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•จ.
  • ์—ญํ• ๋งˆ๋‹ค WSGI application, WSGI Middleware ๋“ฑ์œผ๋กœ ์„ธ๋ถ€์ ์œผ๋กœ ๋‚˜๋‰˜๊ธฐ๋„ํ•จ.

 

ASGI(Asynchronous Server Gateway Interface)

  • WSGI์˜ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋‹จ์ ์„ ๊ฐœ์„ ํ•˜๊ธฐ ๋งŒ๋“ค์–ด์ง„ ๋น„๋™๊ธฐ ์„œ๋ฒ„ ๊ฒŒ์ดํŠธ์›จ์ด ์ธํ„ฐํŽ˜์ด์Šค
  • Uvicorn์ด ASGI์—์„œ ํ™œ์šฉ๋จ.

 

Uvicorn์€ ASGI Web Application์ด๊ณ  ๋™๊ธฐ ๋˜๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์— ๋Œ€ํ•ด ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

FastAPI ์‚ฌ์šฉ ์˜ˆ์‹œ

# ๋™๊ธฐ์„œ๋ฒ„
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
	return {"message":"Hello, FastAPI"}

@app.get("/haha")
def haha():
	return {"message":"Use Uvicorn Server"}

 

uvicorn ํŒŒ์ผ์ฌํŒŒ์ผ:์ธ์Šคํ„ด์Šค๋ช… --reload
# ์ธ์Šคํ„ด์Šค๋ช… : FastAPI()๋กœ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ €์žฅ๋˜๋Š” ๋ณ€์ˆ˜๋ช… 
# --reload๋Š” ์—…๋ฐ์ดํŠธ ๋งˆ๋‹ค reload ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ

 

728x90
๋ฐ˜์‘ํ˜•

'Back-end & Server > FastAPI' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[FastAPI] Form  (0) 2024.02.20
[FastAPI] ๋ชจ๋ธ  (0) 2024.02.20
[FastAPI] ๋งค๊ฐœ๋ณ€์ˆ˜  (0) 2024.02.20
[FastAPI] ๋™์‹œ์„ฑ๊ณผ async / await  (0) 2024.02.19
[FastAPI] ํƒ€์ž… ์ง€์ •, Pydantic  (0) 2024.02.19