threading 함수 실행방법
- Python
- 2023. 1. 3. 21:32
import threading
import time
import logging
# 스레드 실행 함수
def thread_func(name):
logging.info('sub-thread %s: starting', name)
time.sleep(3)
logging.info('sub-thread %s: finishing', name)
if __name__ == "__main__":
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt='%H:%M:%S')
logging.info('main-thread : before creating thread')
# 함수 인자 확인
x = threading.Thread(target=thread_func, args=('First', ))
logging.info('main-thread : before running thread' )
# 서브 스레드 시작
x.start()
# x.join() # # 스레드는 한번 실행되면 메인 로직과 상관없이 바로 끝까지 실행되는데, join 이 있으면, 스레드가 모두 끝난 후 다음 로직이 실행됨
logging.info('main-thread : wait for the thread to finish' )
logging.info('main-thread : all done' )
결과
21:31:47: main-thread : before creating thread
21:31:47: main-thread : before running thread
21:31:47: sub-thread First: starting
21:31:47: main-thread : wait for the thread to finish
21:31:47: main-thread : all done