开发者

Ctrl-C doesn't interrupt semaphore.acquire

开发者 https://www.devze.com 2023-03-04 12:00 出处:网络
while True: try: queries_semaphore.acquire() query = queries.pop(0) 开发者_JAVA技巧# Do some stuff ...
while True:
    try:
        queries_semaphore.acquire()
        query = queries.pop(0)
     开发者_JAVA技巧   # 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消