3
while True:
    try:
        queries_semaphore.acquire()
        query = queries.pop(0)
        # Do some stuff ...
        info('Query executed: `%s\'' % str(query))
    except KeyboardInterrupt:
        okay('quit')
        break

The problem is that KeyboardInterrupt is raised only after queries_semaphore.acquire() returns, so a user isn't able to break the program with Ctrl-C. What's a good solution in this case?

eigenein
  • 2,083
  • 3
  • 25
  • 43

1 Answers1

3

I would create another thread for queries_semaphore.acquire() part and leave main thread for interaction with user. If user hit Ctrl-C then you should unblock working thread by setting semaphore and finish it.

Zuljin
  • 2,612
  • 17
  • 14
  • While this sort of works, the spawned process still makes the main thread wait for the child. So you need to kill that. https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread explains how to do that. – qwerty_so Jan 18 '19 at 18:51