• https://docs.python.org/ko/3/library/asyncio.html
  • import asyncio
    
    async def _async_react_tts_msg_alm(n, alm_dict):
        print(f"_async_react_tts_msg_alm({n}) : tts")
        await asyncio.sleep(n)
        print(f"_async_react_tts_msg_alm({n}) : msg")
        alm_dict[str(n)] = n
        print(f"_async_react_tts_msg_alm({n}) : alm={alm_dict}")
        return
    
    async def _async_react():
        alm_dict = {'ts': time_util.timestamp()}
        t0 = asyncio.create_task(_async_react_tts_msg_alm(4, dict(alm_dict)))
        t1 = asyncio.create_task(_async_react_tts_msg_alm(2, dict(alm_dict)))
        t2 = asyncio.create_task(_async_react_tts_msg_alm(6, dict(alm_dict)))
        await t0
        await t1
        await t2
        
    if __name__ == '__main__':
    	asyncio.run(_async_react())
    	exit(0)
    • create_task
    • gather
    • to_thread
    • run_coroutine_threadsafe
    • ...
    • https://it-eldorado.tistory.com/159
    • ...
  • 차라리? 다른 실타래...
  • import threading
    
    def _thread_reply_tts_msg_log(n, log_dict, wait_list):
        my_logger.info(f"_thread_reply_tts_msg_log({n}) : tts")
        time.sleep(n)
        for wait in wait_list:
            wait.join(timeout=10)
        my_logger.info(f"_thread_reply_tts_msg_log({n}) : msg")
        log_dict[str(n)] = n
        my_logger.info(f"_thread_reply_tts_msg_log({n}) : log={log_dict}")
        return
    
    if __name__ == '__main__':
        thread_list = list()
        t_dict = {'ts': time_util.timestamp()}
    
        t0 = threading.Thread(target=_thread_reply_tts_msg_log, args=(4, dict(t_dict), list(thread_list)))
        t0.daemon = False
        t0.start()
        thread_list.append(t0)
    
        t1 = threading.Thread(target=_thread_reply_tts_msg_log, args=(2, dict(t_dict), list(thread_list)))
        t1.daemon = False
        t1.start()
        thread_list.append(t1)
    
        t2 = threading.Thread(target=_thread_reply_tts_msg_log, args=(6, dict(t_dict), list(thread_list)))
        t2.daemon = False
        t2.start()
        thread_list.append(t2)
        
        exit(0)

-끝-

'랭귀지&프레임웤' 카테고리의 다른 글

FastAPI : Typer (미작성)  (0) 2024.11.30
FastAPI : SQLModel  (0) 2024.11.30
파이썬 메모장  (0) 2023.04.07
파이썬 프로그래밍  (0) 2023.01.29
python ruff & uv  (0) 2022.10.26

+ Recent posts