Python Forum
Cleanest Way to Cancel a ThreadPool - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Cleanest Way to Cancel a ThreadPool (/thread-23088.html)



Cleanest Way to Cancel a ThreadPool - SvetlanaofVodianova - Dec-10-2019

I have a backup script that I run weekly. Each folder that gets copied is independent of the others, one folder doesn't have to wait on the previous folder to finish copying, to start copying.

In an effort to learn asyncio and I decided to modify my backup script with async and a threadpoolexecutor. The problem is when an exception is raised (for example, copying a file produced an error), I want all other tasks to stop, because the backup is no longer valid. A little bit of searching came up with results that don't quite work. A few SO pages warned against stopping a thread, and a few other stated, even if I call task.cancel(), doesn't mean it will get cancelled right way. Synchronously, if an error occurred, the program will crash, and I can investigate. Is a clean method to asynchronously copy a folder with the ability to cancel should an error be raised, possible?