Beispiel #1
0
	def nuke(self):
		# set autodestruct, remove thread from stack and exit thread
		global threads
		try:
			threads.remove([self.threadId,self])
			debugMsg("SUCCESS, I managed to nuke myself")
		except:
			# If this happens Its up to the watchdog to garbage collect this thread...
			debugMsg("WARNING: thread: " + str(self.threadId) + ":" + str(self) + " could not be deleted from list")
		sys.exit()
Beispiel #2
0
	def __init__(self,threadId):
		# Initialize the Threading
		threading.Thread.__init__(self)
		# I use threads_spawned_count in the calling function to name and track
		# my thread, this also used to cleanup the thread and recover memory
		self.threadId = threadId
		# a boolean which says this thread is allowed to live or not
		self.allowedToLive = True
		# Start the timer and call self.nuke when it is reached
		self.t = threading.Timer(thread_timeout,self.nuke)

		self.exectime = randrange(20,45,5)
		debugMsg(str(self) + " will take " + str(self.exectime) + " to complete")
Beispiel #3
0
	def run(self):
		debugMsg(str(self) + " Initializing my timer ")
		self.t.start()
		if self.allowedToLive: 
			#while self.allowedToLive: # if you use while, the self.allowedToLive is critical to ending the while
			debugMsg(str(self) + " EXECUTING ")
			# Just going to sleep for random time, This is where your code executes...
			time.sleep(self.exectime)
			debugMsg(str(self) + " managed to complete its task within timeout")
			self.allowedToLive = False

		# kill via a schedule
		debugMsg(str(self) + " Happily scheduling my own descruction")
		self.t.cancel()
		self.t = threading.Timer(1,self.nuke)
		self.t.start()
Beispiel #4
0
	def run(self):
		try:
			global threads
			global max_threads
			global threads_spawned_count
			while running == True:
				if len(threads) < max_threads:
					try:
						debugMsg(str(self)+" INITIATOR Spawning new thread since len(threads) is less than " + str(max_threads))
						current = someThreadFunction(threads_spawned_count)
						threads.append([threads_spawned_count,current])
						current.start()
						del current
						threads_spawned_count = threads_spawned_count + 1
						debugMsg(str(self)+" INITIATOR Running " + str(len(threads)) + " threads so far")
						print "INITIATOR Active Threads " + str(threading.activeCount()) + " including thread timeout timers"
					except:
						debugMsg("Unable to spawn thread, probably os limit")
						time.sleep(1)
				else:
					debugMsg(str(self)+" INITIATOR Waiting for a thread to timeout / die which will reduce threads_count")
					time.sleep(randrange(2,5,1))
					debugMsg(str(self)+" INITIATOR Running Threads " + str(threads))
					print "INITIATIOR " + str(len(threads)) + " threads in stack"
					print "INITIATIOR Active Threads:" + str(threading.activeCount()) + " including thread timeout timers"
					time.sleep(1)
		except Exception, e:
			debugMsg("WARNING: a initiator has died")
			debugMsg(str(e))
			global initiator_count
			initiator_count = initiator_count - 1
Beispiel #5
0
					print "INITIATIOR Active Threads:" + str(threading.activeCount()) + " including thread timeout timers"
					time.sleep(1)
		except Exception, e:
			debugMsg("WARNING: a initiator has died")
			debugMsg(str(e))
			global initiator_count
			initiator_count = initiator_count - 1
			

	

# Main loop, if you dont need the initiators like I did, you can redirect this to your function directly
while True:
	try:
		if initiator_count < initiators:
			debugMsg("NEW INITIATOR THREAD")
			current = initiatorFunction()
			current.start()
			initiator_count = initiator_count + 1
			initiatorThreads.append(current)
			del current
			time.sleep(0.3)
		else:
			debugMsg(str(initiator_count) + " INITIATORS ALREADY SPAWNED")
			# lets babysit the stack
			for th in threads:
				if not th[1].isAlive():
					debugMsg("I think thread " + str(th) + " is dead because " + str(th[1].isAlive()))
					th[1].nuke
					threads.remove(th)
			time.sleep(10)