라벨이 threading인 게시물 표시

파이썬[Python]: threading - is_alive 메서드

파이썬(Phthon): threading 모듈 - is_alive 함수(function) /// 설명 스레드의 종료 여부를 표현합니다. 스레드가 종료되기 전까지는 True 의 값을 가지고 있습니다. 종료되지 않은 모든 스레드들을 반환하는 enumerate() 함수가 있습니다. ※ 형식 is_alive() reference https://docs.python.org/3/library/threading.html#module-threading /// 예제 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 import  threading import  datetime import  time     def  test_thread(i):      print (f '   ---> test_thread {i} is started!' )     time.sleep(i)      print (f '   {i}: test_thread - sleep({i}) ---' )      print (f '   ---> Time: {datetime.datetime.now().time()}' )     print (f '---> Time: {datetime.datetime.now().time()}' ) t  =  threading.Thread(target = test_thread, args = ( 1 ,)) t.start() print (f '---> Time: {datetime.datetime.now().time()}' ) print ( '-'   *   50 )   time.sleep( 1 )   if  t.is_alive()  = =   True :     

파이썬[Python]: threading - join 메서드

파이썬(Phthon): threading 모듈 - join 함수(function) /// 설명 스레드가 종료되기까지 기다립니다. 이 메서드는 timeout 이 발생되거나 join() 메서드가 호출되는 스레드가 종료하기 전까지 스레드 호출을 차단합니다. 여러번 사용될 수 있습니다. timeout: 실수(float)로 스레드가 종료되는 시간을 명시합니다. 종료 여부는 is_alive()로 확인할 수 있습니다. ※ 형식 join(timeout=None) reference https://docs.python.org/3/library/threading.html#module-threading /// 예제 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 26 27 28 29 30 import  threading import  datetime import  time     def  test_thread(i):      print (f '   ---> test_thread {i} is started!' )     time.sleep(i)      print (f '   {i}: test_thread - sleep({i}) ---' )      print (f '   ---> Time: {datetime.datetime.now().time()}' )     print (f '---> Time: {datetime.datetime.now().time()}' ) t  =  threading.Thread(target = test_thread, args = ( 1 ,)) t.start() print (f '---> Time: {datetime.datetime.now().time()}' ) print ( '-'   *   50 )   t.join() if  t.is_alive()  = =

파이썬[Python]: threading - start 메서드

파이썬(Phthon): threading 모듈 - start 함수(function) /// 설명 스레드를 동작시킵니다. 스레드 객체당 한번만 불리어져야 하며, 객체의 run() 메서드를 조정합니다. ※ 형식 start() reference https://docs.python.org/3/library/threading.html#module-threading /// 예제 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 26 27 28 29 30 import  threading import  datetime import  time     def  test_thread(i):      print (f '   ---> test_thread {i} is started!' )     time.sleep(i)      print (f '   {i}: test_thread - sleep({i}) ---' )      print ( '   ---> Time:' , datetime.datetime.now().time())     print ( '---> Time:' , datetime.datetime.now().time()) t1  =  threading.Thread(target = test_thread, args = ( 1 ,)) t1.start() t2  =  threading.Thread(target = test_thread, args = ( 2 ,)) t2.start() print ( '---> Time:' , datetime.datetime.now().time()) print ( '-'   *   50 )   # ---> Time: 21:03:18.894737 #    ---> test_thread 1 is started! #    ---> test_thr

파이썬[Python]: threading - run 함수

파이썬(Phthon): threading 모듈 - run 함수(function) /// 설명 스레드를 동작시킵니다. Thread 클래스의 하위 클래스를 생성하고 run() 메서드를 작성하셔야 합니다. ---> start() 메서드를 사용하여 스레드를 제어합니다. ※ 형식 run() reference https://docs.python.org/3/library/threading.html#module-threading /// 예제 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import  threading import  datetime import  time     class  TestThread(threading.Thread):      def  __init__( self ,  * args,  * * kwargs):          super (TestThread,  self ).__init__( * args,  * * kwargs)          self .i  =  kwargs[ 'args' ][ 0 ]        def  run( self ):          print (f '   ---> test_thread {self.i} is started!' )         time.sleep( self .i)          print (f '   {self.i}: test_thread - sleep({self.i}) ---' )          print ( '   ---> Time:' , datetime.datetime.now().time())     print ( '---> Time:' , datetime.datetime.now().time()) for  i  in   range ( 1 ,

파이썬[Python]: threading - Thread 클래스

파이썬(Phthon): threading 모듈 - Thread 클래스(class) /// 설명 구분되어진 스레드를 제어하는 클래스입니다.(2가지 방식: 콜러블 객체를 전달, run() 메서드의 오버라이딩) 참고: 스레드 group: 사용하지 않습니다. target: run() 메서드에 의해 시행되는 콜러블 객체입니다. name: 스레드에 이름을 부여합니다. 기본값은 Thread-N, N은 정수 args: target 에 사용되어지는 튜플입니다. kwargs: target 에 사용되어지는 딕셔너리입니다. deamon: the thread is daemonic. (백그라운드에서 시행되는 듯...) ※ 형식 class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None) reference https://docs.python.org/3/library/threading.html#module-threading /// 예제 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 26 27 28 29 30 import  threading import  datetime import  time   def  test_thread(i):      print (f '   ---> test_thread {i} is started!' )     time.sleep(i)      print (f '   {i}: test_thread - sleep({i}) ---' )      print ( '   ---> Time:' , datetime.datetime.now().time())     print ( '---> Time:' , datetime.datetime.now().time()) for  i  in   rang