TIL | c언어 to 파이썬 스레딩 이슈

타샤's 월드·2025년 7월 8일
0

파이썬은 gil 때문에 스레딩이 어렵고, c언어 함수는 당연히 스레딩이 가능하다.

gil이 걸리는 프로세스를 돌릴때 c언어 모듈을 cffi 등 라이브러리로 호출하면 스레딩 이슈 때문에 병목현상이 생길 수 있다

=>해결법?

1) c코드 내에서 gil 해제

2) 파이썬 호출시 gil 해제 (현재 파이선 3.13t에서 가능)

# 그냥 일반 멀티스레드 지만 3.13t에서 gli 없이 작동? 

from threading import Thread
@log_time
def run_multi_thread_task(func: Callable[[Any], Any], input_data: list[Any]) -> None:
    """
    Executes a function in multiple threads concurrently.

    :param func: The function to execute, taking one argument.
    :param input_data: A list of input data that will be passed to the function.
    :return: None
    """
    threads = []
    for data in input_data:
        thread = Thread(target=func, args=(data,))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

근데 자주 부르는것도 아니고 실행시간이 길지 않으면 이렇게까지 할 필요 있을까?
아직 불안정하고 이슈 있을것 같음..

간단한 실험...


profile
그때 그때 꽂힌것 하는 개발블로그

0개의 댓글