¾È±Ô °øºÎ¹æ

Python ÇÁ·Î±×·¡¹Ö > Python - Thread (¾²·¹µå)

µî·ÏÀÏ : 2018-07-02 17:44 Á¶È¸¼ö : 53,564

¾²·¹µå´Â ÇÁ·Î¼¼½º ³»¿¡¼­ µ¶¸³µÈ ½ÇÇà È帧À» °¡Áø Äڵ带 ¸»ÇÕ´Ï´Ù.

ÇϳªÀÇ ÀÛ¾÷À» Çϸ鼭 µ¿½Ã¿¡ ´Ù¸¥ ÀÛ¾÷µµ ÇÒ ¼ö ÀÖ½À´Ï´Ù.

º¸ÅëÀº ÇÑ ÇÁ·Î¼¼½º´ç ÇÑ°³ÀÇ ¾²·¹µå¸¦ °®°í ÀÖÁö¸¸, »óȲ¿¡ µû¶ó¼­´Â ¿©·¯°³ÀÇ ¾²·¹µå¸¦ °¡Áú ¼ö ÀÖ½À´Ï´Ù.

ÀÌ°ÍÀ» ¸ÖƼ¾²·¹µå(Mul-Thread) ¶ó°í ÇÕ´Ï´Ù.
 
import threading
from time import sleep

def Threading(id):
    print("%s - Hello threading" %id)

def thProc(id, sec):
    while True:
        Threading(id)
        sleep(sec)

t1 = threading.Thread(target=thProc, args=("Test1",1))
t1.start()
À§ ÄÚµå´Â 1Ãʸ¶´Ù Test1 - Hello threading ¸¦ Ãâ·ÂÇÏ´Â ¾²·¹µå ÀÔ´Ï´Ù.

t1 °´Ã¼¸¦ ÀÌ¿ëÇØ thProcÀ» ½ÇÇàÇØ ÁÖ¸é while ¹®À» ÅëÇØ ¹«ÇÑ·çÇÁ°¡ ÀÌ·ç¾îÁö°í ±×¿¡ µû¶ó¼­ Threading ÇÔ¼ö°¡ ½ÇÇàµÇ¾î ¹®ÀÚ¿­ÀÌ Ãâ·Â µË´Ï´Ù.
´Ù¸¸ while¹® ¾È¿¡ sleep(sec) ÀÌ ÀÖ°í, sec ¿¡´Â 1ÀÌ Àü´ÞµÇ±â ¶§¹®¿¡ 1Ãʸ¶´Ù Ãâ·ÂµÇ´Â °ÍÀÔ´Ï´Ù.

´ÙÀ½Àº ¸ÖƼ¾²·¹µå¸¦ ½ÇÇàÇÏ´Â ¼Ò½º¸¦ »ðÀÔÇØ º¾´Ï´Ù.
 
import threading
from time import sleep

def Threading(id):
    print("%s - Hello threading" %id)

def thProc(id, sec):
    while True:
        Threading(id)
        sleep(sec)

t1 = threading.Thread(target=thProc, args=("Test1",1))
t2 = threading.Thread(target=thProc, args=("Test2",3))
t1.start()
t2.start()

À̹ø¿¡´Â 3Ãʸ¶´Ù Test2 - Hello threading À» Ãâ·ÂÇÏ´Â ¾²·¹µåµµ »ý¼ºÇß½À´Ï´Ù.

½ÇÇàÈ­¸éÀº ¾Æ·¡¿Í °°½À´Ï´Ù.



 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.