curl / Mailing Lists / curl-users / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

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

From: Dan Fandrich via curl-users <curl-users_at_lists.haxx.se>
Date: Thu, 7 Dec 2023 18:43:53 -0800

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
Received on 2023-12-08