2

I have a infinite loop of 4 jobs like this:

list1 = []
while 1:
    try:
        # job1
        a = B()
        # job2
        c = a.accd()
        # job3
        d = len(c)
        # job4
        list1.append(d)
    except KeyboardInterrupt:
        # save list1 into database(took long time)
        break

After I press Ctrl + C, I can't make sure it does all 4 jobs and then stops.

This seems to work when it is sleeping, but it has the sleep delay.

list1 = []
while 1:
    try:
        # job1
        a = B()
        # job2
        c = a.accd()
        # job3
        d = len(c)
        # job4
        list1.append(d)
    except aaddcdcd:
        # some code here
    finally:
        try:
            time.sleep(3) # if I press Ctrl + C here it works perfectly
        except: KeyboardInterrupt:
            # save list1 into database(took long time)
            break

Is it possible that in any time when I press some key, it does all jobs in this loop, update database and then stops.

jac123
  • 27
  • 6
  • Are the jobs concurrent or sequential? Do they run at the same time or one after another? Not sure this matters. Also are you trying to capture just one KeyBoardInterupt or more of them? – JimmyNJ May 30 '17 at 12:39
  • If the sleep works, then just do ```time.sleep(0.00001)``` or something like that so you don't notice it. – SH7890 May 30 '17 at 13:37
  • The sleep technique is dangerous because you are hoping that the break happens during the sleep... But it might not – le3th4x0rbot May 30 '17 at 15:21
  • @JimmyNJ Thank you for your kind help. I forgot the "break", I just edited the question. – jac123 May 30 '17 at 15:21
  • @SH7890 It works only if you press Ctrl + C in the period when it is sleeping. – jac123 May 30 '17 at 15:27
  • @jac123 okay, yeah, sorry. I misunderstood. – SH7890 May 30 '17 at 16:56

1 Answers1

2

Ok so i have two answers for you.

First answer

def jobOne():
    pass
def jobTwo():
    pass
def jobThree():
    pass
def jobFour():
    pass

interrupted    = False
finished       = False
jobs           = [jobOne, jobTwo, jobThree, jobFour]
jobsCarriedOut = [0] * len(jobs)
currentJob     = 0

while (not finished or not interrupted):
    try:
        jobs[currentJob]()
        jobsCarriedOut[currentJob] += 1

        currentJob += 1

        if currentJob == len(jobs):
            currentJob, finished = 0, True
        else:
            finished = False
    except KeyboardInterrupt:
        interrupted = True

print(jobsCarriedOut)

This piece will exit once a KeyboardInterrupt has been triggered and all jobs have finished.

Second answer

I simply googled disable keyboard interrupt python and found this How can I override the keyboard interrupt? (Python) and came up with this code which is slightly different.

import signal

def signal_handler(signal, frame):
    global interrupted
    interrupted = True

def jobOne():
    pass
def jobTwo():
    pass
def jobThree():
    pass
def jobFour():
    pass

interrupted    = False
finished       = False
jobs           = [jobOne, jobTwo, jobThree, jobFour]
jobsCarriedOut = [0] * len(jobs)
currentJob     = 0

signal.signal(signal.SIGINT, signal_handler)

while (not finished or not interrupted):
    jobs[currentJob]()
    jobsCarriedOut[currentJob] += 1
    currentJob += 1
    if currentJob == len(jobs):
        currentJob, finished = 0, True
    else:
        finished = False

print(jobsCarriedOut)

I have never used the signal library (or even heard of it) so here is the documentation

EDIT I have never used global variables so my usage may be wrong.

EDIT TWO The first example works only 80%~ of the time due to not trapping the error at each step

Joshua Nixon
  • 1,379
  • 2
  • 12
  • 25
  • First answer is not good for the reason you point out. Second answer is good, but I'm not sure why you introduced all the job nonsense. The OP's code is simply a sequence of dependent function calls. Much simpler to just run the straight line code in the body of the loop. – pat May 30 '17 at 17:01
  • @pat i answered before he added what the jobs where – Joshua Nixon May 30 '17 at 21:43