title: terminating a function call in python after n seconds
body: <p>my python code goes like this:</p>
  
  <pre><code>def a():
      ...  
      ...  
      subprocess.call()  
      ...  
      ...  
  
  def b():  
      ...  
      ...  
  </code></pre>
  
  <p>and so on.  </p>
  
  <p>My task:<br>
  1) If <code>subprocess.call()</code> returns within 3 seconds, my execution should continue the moment <code>subprocess.call()</code> returns.<br>
  2) If <code>subprocess.call()</code> does not return within 3 seconds, the <code>subprocess.call()</code> should be terminated and my execution should continue after 3 seconds.<br>
  3) Until <code>subprocess.call()</code> returns or 3 seconds finishes, the further execution should not take place.  </p>
  
  <p>This can be done with threads but how?  </p>
  
  <p>Relevant part of the real code goes like this:   </p>
  
  <pre><code>...  
  cmd = ["gcc", "-O2", srcname, "-o", execname];    
  p = subprocess.Popen(cmd,stderr=errfile)//compiling C program  
  ...  
  ...  
  inputfile=open(input,'w')  
  inputfile.write(scanf_elements)  
  inputfile.close()  
  inputfile=open(input,'r')  
  tempfile=open(temp,'w')
  subprocess.call(["./"+execname,str(commandline_argument)],stdin=inputfile,stdout=tempfile); //executing C program
  tempfile.close()
  inputfile.close()  
  ...  
  ...  
  </code></pre>
  
  <p>I am trying to compile and execute a C program using python.
  When I am executing C program using subprocess.call() and suppose if the C program contains an infinite loop, then the subprocess.call() should be terminated after 3 seconds and the program should continue. I should be able to know whether the subprocess.call() was forcefully terminated or successfully executed so that I can accordingly print the message in the following code.</p>
  
  <p>The back end gcc is of linux.</p>