lundi 29 août 2016

Multiple "finally" clauses/"with" statements added on the fly?

Let's say that I am writing code where I need a fail-safe section that - in case of a failure - will save me from wasting money. For example, code that buys virtual servers via AWS API and since those are paid per hour, it's preferable to shut them down as soon as they stop being useful.

The problem is, I don't know how many such instances I would use and I would create them on the fly, adding them to some list or whatnot. The "destructor" of each of the instance might have an unexpected exception and because of that, I'm afraid that any code with a finally clause would be ugly. I cannot also think of how I would use with because I would be introducing objects on the fly. What are other fail-safe solutions I could use with Python?

Aucun commentaire:

Enregistrer un commentaire