import time
import threading
import subprocess

class Command(object):
	def __init__(self, cmd, inp_path, out_path):
		self.cmd = cmd.split(' ')
		self.process = None
		self.inp_path = inp_path
		self.out_path = out_path

	def run(self, timeout):
		def target():
			f = open(self.inp_path, 'r')
			
			out = open(self.out_path,'w')
			
			self.process = subprocess.Popen(self.cmd,
						   bufsize = 4096,
						   shell = False, 
						   stdin = f, 
						   stdout = out)
			self.process.communicate()
			out.flush()
		return_code = 0
		thread = threading.Thread(target = target)
		thread.start()
		thread.join(timeout)
		if thread.isAlive():
			return_code = 123456
			try:
				self.process.terminate()
	 			thread.join()
	 		except AttributeError:
		 		pass
		else:
		 	return_code = self.process.returncode
		return return_code

c = Command('./a.out', '/tmp/in.txt', '/tmp/out.txt')
return_code = c.run(timeout = 2) #timelimit.

if return_code == 123456:
	print 'TLE' #Time Limit Exceeded
elif return_code == 0:
	print 'Program ran successfully' #Either WA/AC
else:
	print 'RTE' #Run Time Error