3

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 ?

  • This tells you how to catch the signal. https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python – shrewmouse Jun 19 '18 at 15:39
  • @LearningPython The option you mention with setting a flag that is checked each loop, is the recommended way to do this. – Bushman Jun 19 '18 at 15:40
  • You can use such a flag like in a producer worker approach. Or since you have only two threads you can think of a multiprocess pipe. Where you have a parent process and a child process. Then you can send a termination criteria to the child process when it is set in the parent process and both can be terminated http://danielhnyk.cz/python-producers-queue-consumed-by-workers/ – Max Krappmann Jun 19 '18 at 15:41

1 Answers1

1

This uses flags and signals to terminate the infinite loop

#!/usr/bin/env python
import signal
import sys

import time
import random
from threading import Thread

class C1:
   def __init__(self):
      signal.signal(signal.SIGINT,self.signal_handler)
      self.keepgoing = True
      self.list = list()

   def infinite_loop(self):
      while self.keepgoing:
         self.list.append(random.randint(1,10))
         time.sleep(2)

   def signal_handler(self, sig, frame):
       print('You pressed Ctrl+C!')
       self.keepgoing = False

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()
shrewmouse
  • 5,338
  • 3
  • 38
  • 43