Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my python code goes like this:

def a():

def b():

and so on.

My task:
1) If returns within 3 seconds, my execution should continue the moment returns.
2) If does not return within 3 seconds, the should be terminated and my execution should continue after 3 seconds.
3) Until returns or 3 seconds finishes, the further execution should not take place.

This can be done with threads but how?

Relevant part of the real code goes like this:

cmd = ["gcc", srcname, "-o", execname];
p = subprocess.Popen(cmd,stderr=errfile)#compiling C program

           ["./"+execname,str(commandline_argument)],stdin=inputfile,stdout=tempfile);#executing C program


I am trying to compile and execute a C program using python.
When I am executing C program using and suppose if the C program contains an infinite loop, then the should be terminated after 3 seconds and the program should continue. I should be able to know whether the was forcefully terminated or successfully executed so that I can accordingly print the message in the following code.
Updated 17-Jan-15 1:41am

1 solution

Implement some form of timer in your main thread to check whether the call has returned. See[^], for more information.
Share this answer
875ewh84 17-Jan-15 0:44am    
you mean to say:


combining thread.start_new_thread() and Timer, I have tried many permutation and combination but all the above 3 conditions are not getting satisfied.
Richard MacCutchan 17-Jan-15 4:33am    
I have tried many permutation and combination
Then you should update your question and show the exact code, and explain what problems it creates.
875ewh84 17-Jan-15 7:42am    
Question updated.
875ewh84 17-Jan-15 0:50am    
Although 3 conditions are appearing simple,I personally feel that it is difficult to code.
Richard MacCutchan 17-Jan-15 4:34am    
No one ever said it would be easy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900