Python Multi Thread 파이썬 멀티 스레드
Python 2023. 9. 10. 20:26 |멀티 스레드를 사용해 보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import time
import threading
def Job(name, count, delay):
print(f"{name} job started.")
for i in range(count):
print(f"{name} job: {i}.")
time.sleep(delay)
print(f"{name} job finished.")
print("Main started.")
thread_1 = threading.Thread(target=Job, args=("First", 5, 0.5))
thread_1.start()
thread_2 = threading.Thread(target=Job, args=("Second", 5, 0.5))
thread_2.start()
print(f"■ Number of threads: {threading.active_count()}")
time.sleep(1)
print("Main finished.")
|
■ threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)
- target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
- args is a list or tuple of arguments for the target invocation. Defaults to ().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import time
import threading
def Job(name, count, delay):
print(f"{name} job started.")
for i in range(count):
print(f"{name} job: {i}.")
time.sleep(delay)
print(f"{name} job finished.")
print("Main started.")
thread_1 = threading.Thread(target=Job, args=("First", 5, 0.5))
thread_1.daemon = True
thread_1.start()
thread_2 = threading.Thread(target=Job, args=("Second", 5, 0.5))
thread_2.daemon = True
thread_2.start()
print(f"■ Number of threads: {threading.active_count()}")
time.sleep(1)
print("Main finished.")
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import time
import threading
def Job(name, count, delay):
print(f"{name} job started.")
for i in range(count):
print(f"{name} job: {i}.")
time.sleep(delay)
print(f"{name} job finished.")
print("Main started.")
thread_1 = threading.Thread(target=Job, args=("First", 5, 0.5))
thread_1.start()
thread_1.join()
thread_2 = threading.Thread(target=Job, args=("Second", 5, 0.5))
thread_2.start()
thread_2.join()
print(f"■ Number of threads: {threading.active_count()}")
time.sleep(1)
print("Main finished.")
|
■ join(timeout=None)
- Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.
- When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call is_alive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.
- When the timeout argument is not present or None, the operation will block until the thread terminates.
- A thread can be joined many times.
'Python' 카테고리의 다른 글
Python Coroutines and Tasks 파이썬 코루틴과 태스크 (0) | 2023.09.11 |
---|---|
Python Multi Thread Class 파이썬 멀티 스레드 클래스 (0) | 2023.09.10 |
[Pygame] Cursor 파이게임 커서 (0) | 2023.09.10 |
[Pygame] Joystick 파이게임 조이스틱 (0) | 2023.09.06 |
[Pygame] Momentum Based Movement 관성이 적용된 자연스러운 움직임 (0) | 2023.09.05 |