Two approaches:
- try concurrent.futures process executor
Different libraries handle cases like these differently. Might fail more gracefully under your specific circumstances.
- psutils and manual memory allocation logging
If you are building this into a production process, this is a must given errors you’ve encountered so far.