Spaces:
Running
Running
| """ | |
| μ νΈλ¦¬ν° ν¨μ λͺ¨λ | |
| 곡ν΅μΌλ‘ μ¬μ©λλ μ νΈλ¦¬ν° ν¨μλ€μ λͺ¨μλμ λͺ¨λμ λλ€. | |
| """ | |
| 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") | |
| 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) |