2018년 4월 9일 월요일

[python] 스레드 상속하기(subclass)

import time
import threading

class MyThread(threading.Thread):
  def __init__(self, number, style, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.number = number
    self.style = style

  def run(self, *args, **kwargs):
    print('thread starting')
    super().run(*args, **kwargs)
    print('thread has ended')

  def sleeper(num, style):
    print('sleeping for {} {}'.format(num, style))
    time.sleep(num)


t = MyThread(number=3, style='yellow', target=sleeper, args=[3, 'yellow'])
t.start()

댓글 없음 :

댓글 쓰기