I have my code and it does go run to infinity. What I want is that if on the unix command window if the user inputs a ctrl C, I want the program to finish the current loop it in and then come out of the loop. So I want it to break, but I want it to finish the current loop. Is using ctrl C ok? Should I look to a different input?
+3
A:
To do this correctly and exactly as you want it is a bit complicated.
Basically you want to trap the Ctrl-C, setup a flag, and continue until the start of the loop (or the end) where you check that flag. This can be done using the signal
module. Fortunately, somebody has already done that and you can use the code in the example linked.
Edit: Based on your comment below, a typical usage of the class BreakHandler
is:
ih = BreakHandler()
ih.enable()
for x in big_set:
complex_operation_1()
complex_operation_2()
complex_operation_3()
# Check whether there was a break.
if ih.trapped:
# Stop the loop.
break
ih.disable()
# Back to usual operation
Muhammad Alkarouri
2010-08-10 22:11:46
for x in big_set: complex_operation_1() complex_operation_2() complex_operation_3() # Check whether there was a break. if ih.trapped: # Stop the loop. break ih.disable() # Back to usual operation...so for this part the only thing I would need to do to initiate it isif ih.trapped: # Stop the loop. break ih.disable()at the end of my code then?
Yuki
2010-08-10 22:28:11
@Yuki: adding code to comments loses its formatting which messes with Python code, but it looks like you are largely right. You need to do `bh = BreakHandler(); bh.enable()` before the loop, of course.
Muhammad Alkarouri
2010-08-10 23:02:56
I have this: (I don't know at the moment how to turn the code here into a nicer format)ih = BreakHandler() ih.enable() while(True): *Do whatever*if ih.trapped: break ih.disable()
Yuki
2010-08-11 15:03:12
When I run the above code, my code only loops it once even when I didn't press ctrl C.
Yuki
2010-08-11 15:03:46
I implemented everything like you said but when I run it, it is as if there’s a flag implemented even before I can implement the ctrl-c. Thus my loop only goes through 1 cycle.Where does do_cleanup come from and is it necessary?
Yuki
2010-08-11 17:44:05
@Yuki: I will have to go back home before I can test the code and come back to you (I am at the train at the moment). Can you please try replacing `if ih.trapped:` with `if ih._count > 0:` and tell me if it works?
Muhammad Alkarouri
2010-08-11 19:40:04
@Yuki: I have tried the code and it does work as advertised using `if ih.trapped:`. You don't need `do_cleanup`. You probably also need to check the length of your `big_set`, because it obviously sets the maximum number of cycles. If you mean that you want to *repeat the loop* multiple times then you need to put it into another infinite loop and check there for a break, so the `if ih.trapped:` becomes inside the outer loop but outside the inner loop. If you still cannot get it to work I suggest that you modify your question to show us the structure of your code that you want to loop over.
Muhammad Alkarouri
2010-08-11 22:54:31
Okay. So after working on it for a while, I did get it to loop more then once. But here comes the problem. Throughout my code I'm calling os.system commands for unix in loops through all my methods. If I try to do a ctrl c during these os.system inner loops it doesn't do anything. It's as if the problem split into a parent and a child and the parent died but the child kept going. It's dead.
Yuki
2010-08-11 23:44:25
It just hangs there. Here is an example:import timeimport osfrom BreakHandler import BreakHandlerib=BreakHandler()ib.enable()for i in range(400): print ib.count() if ib.count()==1: break for i in range(12): print 'WTH' os.system('generateASM.py -a') print 'yay' time.sleep(4) print 'yay' os.system('cd') time.sleep(4) print 'yay' time.sleep(4) os.system('cd') print ib.count() ib.disable()
Yuki
2010-08-11 23:47:30
it's weird cause it seems like the program just hangs when the os.system occurs in the inner loops.
Yuki
2010-08-11 23:48:09
would I have to implement this class in all the python files I'm using? if so, how would I pass the count?
Yuki
2010-08-12 01:41:29
My first impression is that most of the time you are using `os.system` you do not need to. For example, instead of running a Python file using it you can use `execfile` or `import`. If you want to change the directory then use `os.chdir`. Then if you don't use `os.system` you would have no problem. In short, if you have a program written in multiple python files then using `os.system` is wrong. If you don't know how to do that ask another question on stackoverflow.com and I am sure you will get answers.
Muhammad Alkarouri
2010-08-12 10:20:40