본문 바로가기
지속가능티끌/Python

Python . 스레드 . 함수 , 클래스

by i.got.it 2022. 2. 11.

 

 

 

파이썬 함수 를 스레드로 실행.

- 아래 코드에서 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

 


 

 

 

댓글



 

비트코인




암호화폐       외환/나스닥/골드       암호화폐/외환/나스닥/골드 암호화폐/외환/나스닥/골드   암호화폐/외환/나스닥/골드
     
현물 |선물 인버스 |선물 USDT       전략매니저(카피트레이딩)     롤오버 이자 없는 스왑프리계좌
( 스왑프리 암호화폐도 거래 가능 )    
MT4, MT5 , cTrader 모두 지원     FTMO 계좌 매매운용. MT4,MT5