process
실행 중인 프로그램을 의미한다. 자신만의 메모리를 확보하고 공유하지 않는다.
thread
light weight process 라고도 한다. 1개의 process 내에는 최소 1개의 thread가 존재한다.
multi thread
다수의 thread를 사용하면 물론 결과적으로 실행 속도는 비슷하지만 thread를 번갈아가며 작업하기 때문에 여러개의 작업을 동시에 하는 것처럼 느끼게 할 수 있다.
thread 를 사용하지 않는 경우
이전 작업이 끝나야 다음 작업이 실행된다.
# process : 실행 중인 프로그램을 의미함. 자신만의 메모리를 확보하고 공유하지 않는다.
# thread : light weight process 라고도 함. 1개의 process 내에는 최소 1개의 thread가 존재한다.
# process 내에 여러개의 thread를 운영하여 여러 개의 작업을 동시에 하는 것처럼 느끼게 할 수 있다.
# multi thread로 multi tasking이 가능
import threading, time
def run(id):
for i in range(1, 6):
print('id:{} --> {}'.format(id, i))
time.sleep(0.3)
# thread를 사용하지 않은 경우
run(1)
run(2)
id:1 --> 1
id:1 --> 2
id:1 --> 3
id:1 --> 4
id:1 --> 5
id:1 --> 6
id:1 --> 7
id:1 --> 8
id:1 --> 9
id:1 --> 10
id:2 --> 1
id:2 --> 2
id:2 --> 3
id:2 --> 4
id:2 --> 5
id:2 --> 6
id:2 --> 7
id:2 --> 8
id:2 --> 9
id:2 --> 10
thread 를 사용하는 경우
여러 작업을 번갈아가며 작업한다.
# process : 실행 중인 프로그램을 의미함. 자신만의 메모리를 확보하고 공유하지 않는다.
# thread : light weight process 라고도 함. 1개의 process 내에는 최소 1개의 thread가 존재한다.
# process 내에 여러개의 thread를 운영하여 여러 개의 작업을 동시에 하는 것처럼 느끼게 할 수 있다.
# multi thread로 multi tasking이 가능
import threading, time
def run(id):
for i in range(1, 11):
print('id:{} --> {}'.format(id, i))
time.sleep(0.3)
# thread를 사용하지 않은 경우
# run(1)
# run(2)
# thread를 사용한 경우
# threading.Thread(target=수행함수명)
th1 = threading.Thread(target=run, args=('일',))
th2 = threading.Thread(target=run, args=('둘',))
th1.start()
th2.start()
print('프로그램 종료')
id:일 --> 1
id:둘 --> 1
프로그램 종료
id:둘 --> 2
id:일 --> 2
id:일 --> 3id:둘 --> 3
id:일 --> 4
id:둘 --> 4
id:일 --> 5id:둘 --> 5
id:일 --> 6id:둘 --> 6
id:둘 --> 7
id:일 --> 7
id:둘 --> 8id:일 --> 8
id:둘 --> 9id:일 --> 9
id:일 --> 10id:둘 --> 10
thread의 join() 기능을 사용하는 경우
해당 thread가 종료되기까지 기다려준다.
프로그램 종료 위치에 집중
# process : 실행 중인 프로그램을 의미한다. 자신만의 메모리를 확보하고 공유하지 않는다.
# thread : light weight process 라고도 한다. 1개의 process 내에는 최소 1개의 thread가 존재한다.
# process 내에 여러개의 thread를 운영하여 여러 개의 작업을 동시에 하는 것처럼 느끼게 할 수 있다.
# multi thread로 multi tasking이 가능
import threading, time
def run(id):
for i in range(1, 11):
print('id:{} --> {}'.format(id, i))
time.sleep(0.3)
# thread를 사용하지 않은 경우
# run(1)
# run(2)
# thread를 사용한 경우
# threading.Thread(target=수행함수명)
th1 = threading.Thread(target=run, args=('일',))
th2 = threading.Thread(target=run, args=('둘',))
th1.start()
th2.start()
th1.join()
th2.join()
print('프로그램 종료')
id:일 --> 1
id:둘 --> 1
id:둘 --> 2
id:일 --> 2
id:일 --> 3
id:둘 --> 3
id:둘 --> 4
id:일 --> 4
id:일 --> 5id:둘 --> 5
id:둘 --> 6id:일 --> 6
id:일 --> 7
id:둘 --> 7
id:둘 --> 8id:일 --> 8
id:일 --> 9
id:둘 --> 9
id:둘 --> 10
id:일 --> 10
프로그램 종료
'python' 카테고리의 다른 글
thread - 동기화(줄서기),lock을 활용한 전역변수 충돌 해결 (0) | 2022.10.13 |
---|---|
thread 예시 (시계) (0) | 2022.10.13 |
socket - 기본구조, 메시지 송수신 (0) | 2022.10.12 |
socket - 기본 구조 (0) | 2022.10.12 |
DB 연결 - 기본 db, sql문 예시 (0) | 2022.10.12 |