Skip to content

Instantly share code, notes, and snippets.

@nattybear
Created May 18, 2017 01:02
Show Gist options
  • Save nattybear/f4ed6b775e3f57171a6520afac01eba6 to your computer and use it in GitHub Desktop.
Save nattybear/f4ed6b775e3f57171a6520afac01eba6 to your computer and use it in GitHub Desktop.
import threading
import time
def say(msg):
while True:
time.sleep(1)
print(msg)
for msg in ['you', 'need', 'python']:
t = threading.Thread(target=say, args=(msg,))
t.daemon = True
t.start()
for i in range(100):
time.sleep(0.1)
print(i)
@nattybear
Copy link
Author

nattybear commented May 18, 2017

Sleep 함수

sleep 함수는 인자로 받은 수의 초만큼 동작을 멈추는 거야

예를 들어서 sleep(10)을 실행하면 파이썬이 10초 동안 동작을 멈추지

다음과 같은 상황에서 사용 할 수 있어

화면에 "Hello" 라는 문자열을 1초에 한번씩 10번 출력하고 싶다면 아래와 같이 하면 돼

from time import sleep

for i in range(10):
  print("Hello")
  sleep(1)

Thread

"쓰레드"라고 보통 발음하는데 이건 글로 설명하기 보다는 스카이프로 예제와 함께 설명해줄게!

예고를 하자면

코드는 보통 위에서 아래로 차례대로 실행되는데

쓰레드를 이용하면 두가지 이상의 작업을 동시에 하는 것 처럼 할 수 있어 ㅎ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment