multithread (4) 썸네일형 리스트형 [Python] Thread(5) - Producer and Consumer Using Queue 큐를 이용한 생산자 소비자 구조의 스레드 import concurrent.futures import logging import queue import random import threading import time # 생산자 def producer(queue, event): ''' 네트워크 대기 상태라 가정(서버) ''' while not event.is_set(): message = random.randint(1,101) logging.info('Producer got message: {}'.format(message)) queue.put(message) logging.info('Producer received event Exiting') # 소비자 def consumer(queue, event): '.. [Python] Thread(4) - Lock, Unlock 공유자원을 Lock & unLock 하는 2가지 방식에 대한 정리. import logging from concurrent.futures import ThreadPoolExecutor import time import threading class FakeDataStore: # 공유 변수(value) def __init__(self): # 스택영역 self.value = 0 self._lock = threading.Lock() # 변수 업데이트 함수 def update(self, n): logging.info('Thread ***{}: starting update'.format(n)) # 뮤텍스 & Lock 등 동기화(Thraed synchronization 필요) # Lock 획득 / 방법1 self._.. [Python] Thread(3) - ThreadPoolExecutor import logging from concurrent.futures import ThreadPoolExecutor import time def task(name, n): logging.info('***Sub-Thread %s: starting', name) result = 0 for i in range(n): result += i logging.info('***Sub-Thread %s: finished result: %d', name, result) return result def main(): # Logging format 설정 format = "%(asctime)s: %(message)s" logging.basicConfig(format=format, level=logging.INFO, datefm.. [Python] Thread(1) - Basic import logging import threading import time # 스레드 실행 함수 def thread_func(name): logging.info("**Sub-Thread %s: starting", name) time.sleep(3) logging.info("**Sub-Thread %s: finishing", name) # 메인 영역 if __name__ == "__main__": # Logging format 설정 format = "%(asctime)s: %(message)s" logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S") logging.info("Main-Thread : before creatin.. 이전 1 다음