How to Stop a Terminal Process Running Code Including Threading?

I am using threading to run multiple functions at the same time with the purpose of pulling weather data from multiple websites. I ran these these programs through the Linux terminal, however am unable to cancel them by pressing ctrl C. I want to have them run for an indefinite amount of time, and as such do not want to add in a finish time, but would rather cancel them when necessary. Does anyone know why ctrl C is not cancelling them, and how to fix this?

2 Answers

Daemon Threads

It may be tempting to simply use daemon threads to solve this problem, a python execution will stop when only daemon threads are left, and will not allow them to clean up nicely. The docs call this out clearly:

Daemon threads are abruptly stopped at shutdown. Their resources (such as open files, database transactions, etc.) may not be released properly. If you want your threads to stop gracefully, make them non-daemonic and use a suitable signalling mechanism such as an Event.

Let's follow their advice and use an Event.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.