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

Friday, December 20, 2013

Cross Global Variabel pada Multi File Python

File: file1.py
 #!/usr/bin/env python  
 a=''  
 def test():  
   global a  
   print "Pada Module File1"  
   print "Ini Nilai A: %s \n" %a  

File: file2.py
 #!/usr/bin/env python  
 import file1  
 file1.a="Data1"  
 file1.test()  
 print "Pada Module File2"  

Output: Eksekusi
 hery@ino2Er:~> python file2.py  
 Pada Module File1  
 Ini Nilai A: Data1   
 Pada Module File2  

Keyword: python, seo, multi, cross, file, module

Thursday, December 12, 2013

Parsing HTML, JSS, CSS, dan Quote Python

Kode parsing HTML JSS, CSS, dan Komentar

 def ParsingHtmlManual(finaltext):  
   finaltext=contohweb.split()  
   finaltext=' '.join(finaltext)  
   finaltext = re.sub(r'<script.*?/script>','', finaltext)  
   finaltext = re.sub('<style.*?/style>', '', finaltext)  
   finaltext = re.sub('<!--.*?-->', '', finaltext)  
   finaltext = re.sub('<.*?>', '', finaltext)  
   return finaltext  

Keyword:parsing, seo, html, jss, css, html