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: Hongyi Zhao via curl-users <curl-users_at_lists.haxx.se>
Date: Sun, 10 Dec 2023 12:35:16 +0800

On Sun, Dec 10, 2023 at 10:58 AM Dan Fandrich via curl-users
<curl-users_at_lists.haxx.se> wrote:
>
> On Sat, Dec 09, 2023 at 01:51:13PM +0800, Hongyi Zhao via curl-users wrote:
> > Now, I try to compare the manually computed average speed and the one
> > given by pycurl in the attached script, but the latter always is 0:
> [...]
> > try:
> > c.perform()
> > py_speed = c.getinfo(pycurl.SPEED_DOWNLOAD) / 1024
> > except pycurl.error as e:
> > pass
> > finally:
> > c.close()
>
> Are you sure c.perform() isn't raising an exception? That would leave py_speed
> as 0, as you are observing. Hiding exceptions like this can cause no end of
> problems when things go wrong.

The attached script does the trick:

(datasci) werner_at_X10DAi:~/Desktop$ python rev-3.6.py
Testing Proxy: SG_ssr_futeapbquf5m.nodelist.club_1453_018d83c677e05e71f172014fc3f45e39
Current Speed: 21.04 kB/s
Current Speed: 3726.05 kB/s
Error occurred while performing curl: (28, 'Operation timed out after
5000 milliseconds with 29650632 out of 1073741824 bytes received')
PycURL Speed: 5791.21 kB/s
Average Speed: 5790.76 kB/s
Testing Proxy: HK_ssr_wo8o8npg4fny.nodelist.club_1303_b5bf85111d0f51c517ec7302d3f33ce1
Current Speed: 828.35 kB/s
Current Speed: 5367.13 kB/s
Error occurred while performing curl: (28, 'Operation timed out after
5000 milliseconds with 33954720 out of 1073741824 bytes received')
PycURL Speed: 6631.92 kB/s
Average Speed: 6631.49 kB/s
Testing Proxy: SG_ssr_futeapbquf5m.nodelist.club_1354_ddfba110eddfdb7037f389e9b5917477
^CKeyboardInterrupt at line 72

But I still cannot figure out how to achieve the same purpose with the
following methods:

1. Using the following method suggested by you:

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])


2. Using a pure try-expect-based method.

Regards,
Zhao

> --
> Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-users
> Etiquette: https://curl.se/mail/etiquette.html


-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-users
Etiquette:   https://curl.se/mail/etiquette.html
Received on 2023-12-10