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: Sat, 9 Dec 2023 13:51:13 +0800

On Sat, Dec 9, 2023 at 11:40 AM Ray Satiro via curl-users
<curl-users_at_lists.haxx.se> wrote:
>
> On 12/8/2023 8:49 PM, Hongyi Zhao via curl-users wrote:
>
> On Sat, Dec 9, 2023 at 9:37 AM Hongyi Zhao <hongyi.zhao_at_gmail.com> wrote:
>
> On Fri, Dec 8, 2023 at 10:44 AM Dan Fandrich via curl-users
> <curl-users_at_lists.haxx.se> wrote:
>
> 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.
>
> I tried using the first method of your tips. It works, but the
> calculated *Average Speed* is too high, as shown below:
>
> (datasci) werner_at_X10DAi:~/Desktop$ python callback-SIGINT.py
> Proxy: SG_ssr_futeapbquf5m.nodelist.club_1453_018d83c677e05e71f172014fc3f45e39
> Current Speed: 1371.74 kB/s
> Current Speed: 6565.90 kB/s
> Average Speed: 7552.72 kB/s
> Proxy: HK_ssr_wo8o8npg4fny.nodelist.club_1303_b5bf85111d0f51c517ec7302d3f33ce1
> Current Speed: 1252.55 kB/s
> Current Speed: 7461.93 kB/s
> Average Speed: 8807.27 kB/s
> Proxy: SG_ssr_futeapbquf5m.nodelist.club_1354_ddfba110eddfdb7037f389e9b5917477
> Current Speed: 3081.76 kB/s
> Current Speed: 9351.58 kB/s
> Average Speed: 10745.23 kB/s
> Proxy: HK_ssr_fwqvvo60u1mj.nodelist.club_1423_dba3b0996744e7b82eae7634626a5ba9
> ^Cinterrupted!
> Average Speed: 156.74 kB/s
> Proxy: SG_ssr_futeapbquf5m.nodelist.club_1356_8ce27b1301fcbcb77cc5abb28cb127a6
> Script terminated by user. Exiting.
>
> I'm not sure how to fix this problem. Attached please find the test script.
>
> I tried another method which also had the same problem:
>
> (datasci) werner_at_X10DAi:~/Desktop$ python rev-2.py
> Proxy: SG_ssr_futeapbquf5m.nodelist.club_1453_018d83c677e05e71f172014fc3f45e39
> Current Speed: 664.56 kB/s
> Current Speed: 7691.08 kB/s
> Average Speed: 9403.12 kB/s
> Proxy: HK_ssr_wo8o8npg4fny.nodelist.club_1303_b5bf85111d0f51c517ec7302d3f33ce1
> Current Speed: 950.39 kB/s
> Current Speed: 6146.08 kB/s
> Average Speed: 7297.46 kB/s
> Proxy: SG_ssr_futeapbquf5m.nodelist.club_1354_ddfba110eddfdb7037f389e9b5917477
> ^CTraceback (most recent call last):
> File "/home/werner/Desktop/rev-2.py", line 60, in progress
> def progress(download_t, download_d, upload_t, upload_d):
>
> KeyboardInterrupt
>
>
> As far as I can tell you have some issues using Python and have been receiving help in a Python discussion thread [1]. How does curl factor into this except that the average speed looks wrong to you?
>
> Your code does:
>
> average_speed = c.getinfo(pycurl.SPEED_DOWNLOAD) / 1024
>
> That should be ok. You are calculating the speed in KiB (though a lot of people will abbreviate it as KB), and not kB (notice the lowercase 'k'). If you suspect that average speed pycurl is giving you is wrong then you could compare against the curl tool.
>
> For example (based off your python code):
>
> curl --max-time 5 --proxy socks5h://SG_ssr_futeapbquf5m.nodelist.club_1453_018d83c677e05e71f172014fc3f45e39 -o /dev/null http://ipv4.download.thinkbroadband.com/1GB.zip
>
> that would show progress information as the file is being downloaded (to /dev/null) and the average speed.
>
>
> [1]: https://discuss.python.org/t/making-python-script-exit-completely-with-one-ctrl-c/40529/1

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:

(datasci) werner_at_X10DAi:~/Desktop$ python rev-3.py
Testing Proxy: SG_ssr_futeapbquf5m.nodelist.club_1453_018d83c677e05e71f172014fc3f45e39
Current Speed: 75.63 kB/s
Current Speed: 5395.45 kB/s
PycURL Speed: 0.00 kB/s
Average Speed: 4389.14 kB/s
Testing Proxy: HK_ssr_wo8o8npg4fny.nodelist.club_1303_b5bf85111d0f51c517ec7302d3f33ce1
Current Speed: 2138.56 kB/s
Current Speed: 7901.99 kB/s
PycURL Speed: 0.00 kB/s
Average Speed: 6323.33 kB/s
Testing Proxy: SG_ssr_futeapbquf5m.nodelist.club_1354_ddfba110eddfdb7037f389e9b5917477
Current Speed: 937.87 kB/s
Current Speed: 7540.93 kB/s
PycURL Speed: 0.00 kB/s
Average Speed: 6209.84 kB/s
Testing Proxy: HK_ssr_fwqvvo60u1mj.nodelist.club_1423_dba3b0996744e7b82eae7634626a5ba9
^CKeyboardInterrupt at line 70

I don't know how to fix this problem. Any tips will be appreciated.

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-09