πŸ–₯ 파이썬 μŠ€λ ˆλ“œμ— λŒ€ν•œ 이해

may_soouuΒ·2020λ…„ 12μ›” 22일
0
post-thumbnail

1. μŠ€λ ˆλ“œλž€?

ν”„λ‘œμ„ΈμŠ€μ˜ μ‹€ν–‰λ‹¨μœ„μž…λ‹ˆλ‹€.
ν”„λ‘œμ„ΈμŠ€λŠ” μ΅œμ†Œ ν•˜λ‚˜ μ΄μƒμ˜ μŠ€λ ˆλ“œλ₯Ό κ°€μ§‘λ‹ˆλ‹€.
ν”„λ‘œμ„ΈμŠ€ : ν”„λ‘œκ·Έλž¨μ΄ λ©”λͺ¨λ¦¬μ— μ˜¬λΌκ°€μ„œ μ‹€ν–‰ 쀑인 것

2. μŠ€λ ˆλ”© λͺ¨λ“ˆ

νŒŒμ΄μ¬μ—μ„œλŠ” 단 ν•˜λ‚˜μ˜ 메인 μŠ€λ ˆλ“œλ§Œ μ‘΄μž¬ν•©λ‹ˆλ‹€.
κ·Έλž˜μ„œ λ©€ν‹° μŠ€λ ˆλ“œλ₯Ό μ΄μš©ν•˜λ €λ©΄ threading λͺ¨λ“ˆμ„ ν™œμš©ν•΄μ•Όν•©λ‹ˆλ‹€.
λ©€ν‹° μŠ€λ ˆλ“œ : ν•˜λ‚˜μ˜ ν”„λ‘œμ„ΈμŠ€ μ•ˆμ— μŠ€λ ˆλ“œκ°€ μ—¬λŸ¬κ°œ

μŠ€λ ˆλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ 두 가지 μ΄μƒμ˜ ν•¨μˆ˜λ₯Ό λ™μ‹œμ— λ™μž‘μ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

πŸ“Œ λ©€ν‹° μŠ€λ ˆλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄, μŠ€λ ˆλ“œ κ°„ 데이터 곡유 λ³΅μž‘λ„κ°€ μ¦κ°€ν•©λ‹ˆλ‹€.
그렇기에 락과 λ°λ“œ 락에도 신경을 μ¨μ•Όν•©λ‹ˆλ‹€.
락 : μžμ›μ— λŒ€ν•œ 접근을 μ œν•œν•˜λŠ” λ©”μ»€λ‹ˆμ¦˜
λ°λ“œ 락 : μ„œλ‘œ κ°„μ˜ 락이 λ¬Άμ—¬ 있음

락을 κ΄€λ¦¬ν•˜λ €λ©΄?

queue λͺ¨λ“ˆμ„ μ‚¬μš©ν•΄μ•Όν•©λ‹ˆλ‹€. queueλŠ” FIFO λ°©μ‹μœΌλ‘œ μŠ€λ ˆλ“œκ°€ μžˆλŠ” λ™μ•ˆμ—λŠ” ν”„λ‘œκ·Έλž¨ μ’…λ£Œκ°€ λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

import threading
import time
 # μŠ€λ ˆλ“œ λͺ¨λ“ˆ μž„ν¬νŠΈ

class Worker(threading.Thread):
# threadingμ—μ„œ λ‚΄μž₯ λͺ¨λ“ˆμΈ Threadλ₯Ό 상속
    def __init__(self, name):
        super().__init__()
        self.name = name            

    def run(self):
    # μŠ€λ ˆλ“œλ₯Ό κ΅¬λ™ν•˜κΈ° μœ„ν•΄μ„œλŠ” ν•¨μˆ˜λͺ…을 run으둜 해야함
    # start() λ©”μ†Œλ“œλ‘œ thread 객체 μ‹€ν–‰
        print("sub thread start ", threading.currentThread().getName())
        time.sleep(3)
        print("sub thread end ", threading.currentThread().getName())


print("main thread start")
for i in range(5):
    name = "thread {}".format(i)
    t = Worker(name)                # sub thread 생성
    t.start()                       # sub thread의 run λ©”μ„œλ“œλ₯Ό 호좜

print("main thread end")

3. 데λͺ¬ μŠ€λ ˆλ“œ

데λͺ¬ μŠ€λ ˆλ“œλž€ 메인 μŠ€λ ˆλ“œκ°€ μ’…λ£Œλ  λ•Œ, 본인의 μ‹€ν–‰ μƒνƒœμ™€ 상관없이 μ’…λ£Œλ˜λŠ” μ„œλΈŒ μŠ€λ ˆλ“œμž…λ‹ˆλ‹€.
(ex. μ„œλΈŒ μŠ€λ ˆλ“œλ₯Ό 톡해 νŒŒμΌμ„ λ™μ‹œμ— λ‹€μš΄λ‘œλ“œ λ°›λŠ”λ°, λ‹€μš΄ λ°›λŠ” μ‚¬μ΄νŠΈ 자체(메인 μŠ€λ ˆλ“œ)λ₯Ό μ’…λ£Œν•˜λ©΄ λ‹€μš΄ λ°›κ³  있던 파일(μ„œλΈŒ μŠ€λ ˆλ“œ)도 정지 된 채 같이 μ’…λ£ŒλœλŠ 것)

데λͺ¬ μŠ€λ ˆλ“œλ₯Ό μ„€μ •ν•˜λ €λ©΄

threading λͺ¨λ“ˆμ—μ„œ daemon 속성을 True둜 두면 λ©λ‹ˆλ‹€.

μœ„μ˜ μ½”λ“œμ—μ„œ daemon 속성을 μ€€ λ’€, λ‘κ°œ μ½”λ“œλ₯Ό λΉ„κ΅ν•΄μ„œ 싀행해보면 차이λ₯Ό μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

import threading
import time


class Worker(threading.Thread):
    def __init__(self, name):
        super().__init__()
        self.name = name           

    def run(self):
        print("sub thread start ", threading.currentThread().getName())
        time.sleep(3)
        print("sub thread end ", threading.currentThread().getName())


print("main thread start")
for i in range(5):
    name = "thread {}".format(i)
    t = Worker(name)                # sub thread 생성
    t.daemon = True	            # 데λͺ¬ μŠ€λ ˆλ“œ 
    t.start()                       # sub thread의 run λ©”μ„œλ“œλ₯Ό 호좜

print("main thread end")

μ°Έκ³  μ‚¬μ΄νŠΈ

profile
back-end 개발자

2개의 λŒ“κΈ€

comment-user-thumbnail
2021λ…„ 1μ›” 23일

잘보고 κ°‘λ‹ˆλ‹€

1개의 λ‹΅κΈ€