[Python] Concurrency PDF 파일 생성 - 2
·
Python/Python
PDF 파일 생성 PDF 파일 생성은 비동기 I/O 작업이다. 평벙하고 쉽게 누구나처럼 그냥 함수 혹은 클래스 작성해서 반복문으로 구현할 수도 있지만, 정말로 업무가 급하고 그런게 아니라고 한다면, 빠르게 동작을 할 수 있도록 하자. Sample Data와 Package import random import uuid from threading import Thread from faker import Faker from fpdf import FPDF fake = Faker() class Person: # slots는 Class attr에 빠른 접근 및 제어, 적은 메모리 사용에 이점이 있다. __slots__ = ['name', 'age', 'location'] def __init__(self, name:..
[Python] Concurrency 어떤 경우에 어떤 것을 사용하는게 좋을까 - 1
·
Python/Python
어느 상황에 어떤 Concurrency API를 사용하는게 좋을까 우리는 평소 개발 업무를 하면서 최대한 Response latency, Memory Usage 등을 줄이기 위해 노력을 한다. 예를 들어, ORM을 사용하는 경우 N+1 이나 1+N+a 등 Query Fetch를 줄이거나 또는, Algorithm을 이용해 시간복잡도를 최대한 적게 할려고 하는 등 말이다. 그럼 Python에서 비동기 소스를 개발할 때는 어떠한 경우에 어떻게 해야 좋은지 알아보자. 우선 Python이 제공하는 concurrency API는 다음과 같다. Coroutine: asyncio module Thread: threading module Process: multiprocessing module 이 3가지의 module ..
[Python] Thread와 Async를 이용한 비동기 방법
·
Python/Python
Python Thread와 Async를 이용한 비동기 방법 이후 Client에서 Request 보낼 Server의 소스는 다음과 같다. @router.get("/second") async def second(): return { "second": random.randint(1, 100), } 그리고 일반 함수를 생성해서 Server로 Request 했을 때, 약 20초 걸린다. import datetime import requests def second_request(): resp = requests.get("http://localhost:8090/second") start = datetime.datetime.now() for _ in range(0, 10): second_request() print(d..