Socialabel

Saturday, December 21, 2013

Menghentikan multithreading yang running dengan waktu tertentu

Menghentikan sebuah thread yang sedang running selama waktu tertentu dengan memanfaatkan module multiprocessing

Kode: thread.py
 #!/usr/bin/env python  
 import multiprocessing  
 import time  

 def get_page(*args, **kwargs):  
   print "Data "  
   if args[0]=="A":  
     time.sleep(2)  
   elif args[0]=="B":  
     time.sleep(5)  
   else:  
     time.sleep(10)  
   print "Nilai ->", args  

 def start_get_page(timeout, *args, **kwargs):  
   p = multiprocessing.Process(target=get_page, args=args, kwargs=kwargs)  
   p.start()  
   p.join(timeout)  
   if p.is_alive():  
     # stop the downloading 'thread'  
     p.terminate()  
     # and then do any post-error processing here  
   print "End"  

 if __name__ == "__main__":  
   start_get_page(4,"A","A")  
   start_get_page(4,"B","B")  
   start_get_page(12,"C","C")  

Sumber : Klik
Keyword: thread, multithreading, python, matikan, hentikan, stop, threading, tesis, seo, python menghentikan thread yang running berdasarkan waktu, lama waktu, durasi waktu python

No comments:

Post a Comment