Re: Making Python Script Exit Completely with One Ctrl+C

Dan Fandrich via curl-users <[email protected]>
Newsgroups gmane.comp.web.curl.general
Message-ID <[email protected]>
On Fri, Dec 08, 2023 at 09:56:44AM +0800, Hongyi Zhao via curl-users wrote:
> I tried the following method but in vain:
[...]
>     try:
>         pass
>     except KeyboardInterrupt:
>         kb_interrupt = True

This won't accomplish anything because "pass" will never raise an exception.
I have a feeling that even putting the entire contents of the progress callback
into a try/except block won't even help; I suspect the exception is raised the
moment the Python interpreter is returned to by pycurl while calling that
callback, which is before even that point.

You'll probably have to block SIGINT before calling calling into pycurl, then
unblocking in the progress callback to check it, e.g.

...
signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGINT])
c.perform()
signal.pthread_sigmask(signal.SIG_UNBLOCK, [signal.SIGINT])
...

def progress(...):
    try:
        signal.pthread_sigmask(signal.SIG_UNBLOCK, [signal.SIGINT])
    except KeyboardInterrupt:
        print('interrupted!')
        return 1
    finally:
        signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGINT])

or just set your own SIGINT handler that sets a flag (kb_interrupt), then check
that flag in the progress callback and return an error if it's set.
-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-users
Etiquette:   https://curl.se/mail/etiquette.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.