본문 바로가기

thread

(5)
[Python] I/O Bound(2) - Multithreading 통신관련 멀티스레드 처리(동기방식) import concurrent.futures import threading import requests import time # 각 스레드에 생성되는 객체(독립된 네임스페이스) thread_local = threading.local() # 세션 제공 def get_session(): if not hasattr(thread_local, "session"): # 딕셔너리 타입으로 확인 thread_local.session = requests.Session() print('>>> not hasattr()') return thread_local.session def request_site(url): # 세션 획득 session = get_session() # 세션 확인 pr..
[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(2) - DeamonThread """ DaemonThread(데몬스레드) (1).백그라운드에서 실행 (2).메인스레드 종료시 즉시 종료 - 데몬스레드의 특징 (3).주로 백그라운드 무한 대기 시 이벤트 발생시 실행하는 부분 담당 -> JVM(가비지 콜렉터), 자동저장 (4).일반 스레드는 작업 종료시 까지 실행 """ import logging import threading import time # 스레드 실행 함수 def thread_func(name, d): logging.info("Sub-Thread %s: starting", name) for i in d: print("***Sub-Thread {}: {}".format(name, i)) # pass logging.info("Sub-Thread %s: finishing", na..
[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..