파이썬 함수 를 스레드로 실행.
- 아래 코드에서 my_thread.start() 실행하면 함수 my_function 이 스레드로 실행된다.
import threading
def my_function():
while True:
print('endless')
my_thread = threading.Thread(target = my_function)
# my_thread.daemon 을 True로 설정하면 메인프로세스 종료된 경우 스레드도 같이 종료되게 하는 설정.
# 기본값은 False 로 되어있음.
#my_thread.daemon = True
my_thread.start()
파이썬 클래스 를 스레드 클래스에서 상속 받기
- 클래스 threading.Thread 를 상속받아, 내 클래스를 만들고, threading.Thread 에 있는 run 을 내 클래스에서 오버라이딩 한다.
import threading
class my_class(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self): # threading.Thread 의 run 을 오버라이딩 한것.
while True:
print('endless')
my_class_obj1 = my_class()
#my_class_obj1.daemon = True
my_class_obj1.start() # my_class 의 run 이 스레드로 실행됨.
연관
Python. 타이머. 일정 시간격 함수 반복 실행 편리.
아래 예는 my_func() 이 1초단위로 무한 반복 실행됨. # 파이썬 타이머 활용예 import threading def my_func(): print('my_func') # 인자1. 초단위시간. 인자2. 해당시간시 수행할 함수. start() 호출이후 인자1..
igotit.tistory.com
Python. 클래스
파이썬의 클래스 멤버함수 - 클래스 멤버함수 정의시 첫인자 필수 기록해야하며 멤버함수 호출시에 이 인자에 값 전달하지 않음. 통상 인자명을 self 로 함. - self 인자는 C++ 클래스에서의 this 와
igotit.tistory.com
첫 등록 : 2022.02.11
최종 수정 :
단축 주소 : https://igotit.tistory.com/3469
'지속가능티끌 > Python' 카테고리의 다른 글
Python . 포터블 개발환경 구축 (0) | 2024.08.16 |
---|---|
Google Colab(Colaboratory) . 웹기반 파이썬 코딩 실행 (0) | 2024.08.09 |
파이썬 . PYTHONPATH 환경변수 . import 검색 경로 추가 (0) | 2022.02.11 |
Python . 실행시 인자 전달 (0) | 2022.01.28 |
Python . Visual Studio 파이썬 한글 깨짐 해결책 (0) | 2022.01.27 |
댓글