threading & multiprocessing

프로세스와 쓰레드의 차이점에 대해서 아주 간단히 짚고 넘어가 봅니다.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6b30b135-bb9b-4d46-9aaf-b773df5e5646/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6b30b135-bb9b-4d46-9aaf-b773df5e5646/Untitled.png

image from : Difference Between Process And Thread in Linux

python threading/multiprocessing 예제

import time
from threading import Thread

def work(id, start, end, result):
    total = 0
    for i in range(start, end):
        total += i
    result.append(total)
    return

# if __name__ == "__main__":
#     START, END = 0, 100000000
#     result = list()
#     th1 = Thread(target=work, args=(1, START, END, result))
    
#     start = time.time()
#     th1.start()
#     th1.join()

if __name__ == "__main__":
    START, END = 0, 100000000
    result = list()
    th1 = Thread(target=work, args=(1, START, END//2, result))
    th2 = Thread(target=work, args=(2, END//2, END, result))
    
    start = time.time()
    th1.start()
    th2.start()
    th1.join()
    th2.join()

print(f"Result: {sum(result)}")
print("time :", time.time() - start)

Common/multiprocess_kb.py

# 키보드가 눌린 횟수
key_value_default = {'w': 0, 'a': 0, 's': 0, 'd': 0, 'q': 0, 'e': 0, 'move': False }
# 조종값으로 변환된 결과
control_offset = {'IDstepLength': 0.0, 'IDstepWidth': 0.0, 'IDstepAlpha': 0.0, 'StartStepping': False }
while True:
	if keyboard.is_pressed('w'):
	    if not was_pressed:
	        self.keyCounter('w')
	        was_pressed = True
KeyTest = KeyInterrupt()
  KeyProcess = Process(target=KeyTest.keyInterrupt, args=(1, KeyTest.key_status, KeyTest.command_status))

  KeyProcess.start()

  testWhile(2, KeyTest.command_status)