Last active
          August 29, 2015 14:11 
        
      - 
      
 - 
        
Save k3170makan/6448a9db90d93e45e0e3 to your computer and use it in GitHub Desktop.  
    Hacked up thread management example in python
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/python | |
| import threading | |
| from sys import argv | |
| from sys import exit | |
| """ | |
| **Please note this class finds factors not prime factors, I cut the example short because its 1am in the morning here (-__-) | |
| Spawn a whole bunch of threads from a single process and check if each have completed their task | |
| """ | |
| class StoppableThread(threading.Thread): | |
| def __init__(self): | |
| super(StoppableThread,self).__init__() | |
| self._stop = threading.Event() | |
| def stop(self): | |
| self._stop.set() | |
| raise Exception("stopped") # a little hack to make sure they stop | |
| def stopped(self): | |
| return self._stop.isSet() | |
| """ | |
| you should implement your own thread objects and inherit the class above, as in the PrimeCracker thead objects ;) | |
| A simple factorization thread object, for those | |
| of us who like cracking prime numbers :) | |
| """ | |
| class PrimeCracker(StoppableThread): | |
| def __init__(self,ID,search_range,target): | |
| super(PrimeCracker,self).__init__() | |
| self.search_range = search_range | |
| self.target = target | |
| self.ID = ID | |
| def run(self): | |
| print "[*] {thread %d} reporting for duty!..." % (self.ID) | |
| for i in self.search_range:#stupid way of checking factors | |
| if self.target%i == 0: | |
| print "[!] {thread %d} found factor! [%d]" % (self.ID,i) | |
| self.sepuku() #lols its funny if you know what sepuku is ;) | |
| def sepuku(self): | |
| #read him is rights <--- whatever clean up code you need should code here i.e. releasing file locks, freeing shared memory blah blah | |
| try: | |
| self.stop() | |
| except: | |
| pass | |
| if __name__=="__main__": | |
| """ | |
| We're gonna take a big composite number and divide the number | |
| ranges below prime/2 into 10 ranges and pass each range to a | |
| thread to check for factors. Once a thread has found a factor we kill it. | |
| """ | |
| big_comp = int(argv[1]) | |
| ranges = [range(i,i+big_comp/20) for i in range(1,big_comp/2,big_comp/20)] #its pretty stupid I know but you could prune the lists for interesting numbers instead of just passing it a contigious list, also range generates a uhm a generator which is quiet memory efficient (as of Python 2.7) | |
| #run through the ranges | |
| threads = [PrimeCracker(index,factor_range,big_comp) for index,factor_range in enumerate(ranges)] | |
| #thread manager | |
| while True: | |
| for thread in threads: | |
| try: | |
| if not(thread.is_alive()): | |
| thread.start() | |
| except RuntimeError,e: | |
| if threading.active_count() <= 1: | |
| exit() #all the threads are done :) | |
| #if the active count is not down, some threads are still exiting, lets give them some time | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment