"프로그래밍 언어의 개념과 흐름에 대한 고찰 - 파이썬답게 코딩하기" 학습중...


동시성과 관련한 thread 기법에 대해 어렴풋이 알거나 몰랐던 내용들 몇가지 정리해봅니다.



Thread Event


# ## Thread Event
# import time
# import logging
# import threading
#
# logging.basicConfig(level = logging.DEBUG, format = "(%(threadName)s) %(message)s")
#
# # >>> e = threading.Event()
# # >>> e.isSet()
# # False
# # >>> e.set()
# # >>> e.isSet()
# # True
# # >>> e.clear()
# # >>> e.isSet()
# # False
#
# def first_wait(e1, e2) :
# while not e1.isSet() :
# event = e1.wait(1)
# logging.debug("Event Status : (%s)", event) ## logging에서 message 출력 부분
#
# if event :
# logging.debug("e1 is set.")
# time.sleep(3)
# logging.debug("Set e2")
# e2.set()
#
# def second_wait(e2) :
# while not e2.isSet() :
# event = e2.wait(1)
# logging.debug("Event Status : (%s)", event) ## logging에서 message 출력 부분
#
# if event :
# logging.debug("e2 is set.")
#
# def main() :
# e1 = threading.Event()
# e2 = threading.Event()
#
# t1 = threading.Thread(name = 'first', target = first_wait, args = (e1, e2)) ## logging에서 threadName = name = first
# t1.start()
#
# t2 = threading.Thread(name = 'second', target = second_wait, args = (e2,))
# t2.start()
#
# logging.debug("wait...")
# time.sleep(5)
# logging.debug("Set e1")
# e1.set()
# time.sleep(5)
# logging.debug("exit")
#
# if __name__ =="__main__" :
# main()

# (MainThread) wait...
# (second) Event Status : (False)
# (first) Event Status : (False)
# (first) Event Status : (False)
# (second) Event Status : (False)
# (second) Event Status : (False)
# (first) Event Status : (False)
# (second) Event Status : (False)
# (first) Event Status : (False)
# (MainThread) Set e1
# (first) Event Status : (True)
# (first) e1 is set.
# (second) Event Status : (False)
# (second) Event Status : (False)
# (second) Event Status : (False)
# (first) Set e2
# (second) Event Status : (True)
# (second) e2 is set.
# (MainThread) exit




Thread Lock


## Thread Lock

# import time
# import logging
# import threading
#
# logging.basicConfig(level = logging.DEBUG, format = "(%(threadName)s) %(message)s")
#
# def blocking_lock(lock) :
# logging.debug("start blocking lock")
#
# while True :
# time.sleep(1)
# lock.acquire() ## .acquire() 메서드가 lock을 잡기 위한 메서드인데 아무런 인자값을 주지 않으면 잡을 때까지 block.
# try :
# logging.debug("grab it")
# time.sleep(0.5)
# finally :
# logging.debug("release")
# lock.release()
#
# # python threading 패키지에서는 Lock을 지원한다.
# # lock acquire하면 해당 쓰레드만 공유 데이터에 접근할 수 있고,
# # lock release 해야 다른 쓰레드에서 공유 데이터에 접근할 수 있다.
#
# def nonblocking_lock(lock) :
# logging.debug("start nonblocking lock")
# attempt = 0
# grab = 0
# while grab < 3 :
# time.sleep(1)
# logging.debug("attempt")
# success = lock.acquire(False) ## acquire 인자로 False를 전달하여 이 쓰레드는 block되지 않고 다음 로직을 수행.
# try :
# attempt += 1
# if success : ## acquire를 하게 되면 True를 반환하여 이하 구문을 실행.
# logging.debug("grab it")
# grab += 1
# finally :
# if success :
# logging.debug("release")
# lock.release()
#
# logging.debug("attempt : %s, grab : %s" % (attempt, grab))
#
# def main():
# lock = threading.Lock()
# blocking = threading.Thread(target = blocking_lock, name = "blocking", args = (lock,))
# blocking.setDaemon(True)
# blocking.start()
#
# nonblocking = threading.Thread(target = nonblocking_lock, name = "nonblocking", args = (lock,))
# nonblocking.start()
#
# if __name__ == "__main__" :
# main()
#
# # (blocking) start blocking lock
# # (nonblocking) start nonblocking lock
# # (nonblocking) attempt
# # (blocking) grab it
# # (blocking) release
# # (nonblocking) attempt
# # (nonblocking) grab it
# # (nonblocking) release
# # (blocking) grab it
# # (nonblocking) attempt
# # (blocking) release
# # (nonblocking) attempt
# # (nonblocking) grab it
# # (nonblocking) release
# # (blocking) grab it
# # (blocking) release
# # (nonblocking) attempt
# # (nonblocking) grab it
# # (nonblocking) release
# # (nonblocking) attempt : 5, grab : 3



'파이썬(PYTHON)' 카테고리의 다른 글

파이썬의 스레드(thread)4  (0) 2020.08.27
파이썬의 스레드(thread)3  (0) 2020.08.27
파이썬의 스레드(thread)1  (0) 2020.08.24
파이썬의 기본 문법3  (0) 2020.08.23
파이썬의 기본 문법2  (0) 2020.08.23

+ Recent posts