""" 유틸리티 함수 모듈 공통으로 사용되는 유틸리티 함수들을 모아놓은 모듈입니다. """ import os import fcntl import pytz from contextlib import contextmanager from datetime import datetime # 한국 시간대 설정 KOREA_TZ = pytz.timezone('Asia/Seoul') def get_korea_datetime_now(): """한국 시간대의 현재 시간을 반환""" return datetime.now(KOREA_TZ) def get_current_datetime_str(dt=None): """한국 시간대의 시간을 문자열로 포맷""" if dt is None: dt = get_korea_datetime_now() return dt.strftime('%Y-%m-%d %H:%M:%S') def get_current_date_str(): """현재 날짜를 한국 시간으로 반환""" return get_korea_datetime_now().strftime("%Y-%m-%d") @contextmanager def file_lock(lock_file_path): """ 파일 기반 배타적 잠금을 제공하는 context manager Args: lock_file_path: 잠금 파일 경로 Yields: None (맥락 관리자로만 사용) Examples: >>> with file_lock('/tmp/test.lock'): ... # 잠금이 걸린 상태에서 작업 수행 ... pass """ # 잠금 파일이 없으면 생성 if not os.path.exists(lock_file_path): open(lock_file_path, 'w').close() # 잠금 파일을 열고 배타적 잠금 획득 with open(lock_file_path, 'r') as lock_file: try: # 배타적 잠금 시도 (다른 프로세스가 대기) fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX) # 잠금 획득 성공, 작업 수행 yield finally: # 잠금 해제 fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)