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.