Here is the code snippet for illustration purpose. C1.infinite_loop() method is running as background thread. I want to make sure if somebody presses Ctrl+C then terminate the program gracefully. What method do i need to override and where to handle Ctrl+C signal ? I am thinking to set a flag when Ctrl+C is generated and periodically check inside C1.infinite_loop. If flag is true then come out of the loop ? Is this right approach or do you suggest something else ?
import time
import random
from threading import Thread
class C1:
   def __init__(self):
     self.list = list()
   def infinite_loop(self):
     while True:
       self.list.append(random.randint(1,10))
       time.sleep(2)
class C2:
   def __init__(self):
       print('inside C2 init')
       self.c1 = C1()
   def main(self):
      self.bg_th = Thread(target=self.c1.infinite_loop)
      self.bg_th.start()
   def disp(self):
      print(self.c1.list)
c2 = C2()
c2.main()
time.sleep(2)
c2.disp()
c2.bg_th.join()
One more question. List is shared between two threads here. C2 is reading and C1 is writing. Do i still need to use lock in such a case ?
 
    