Error handling EAFP如何避免LBYL中TOC/TOU竞争条件的风险?

Error handling EAFP如何避免LBYL中TOC/TOU竞争条件的风险?,error-handling,race-condition,control-flow,Error Handling,Race Condition,Control Flow,与LBYL(三思而后行)相比,EAFP(更容易请求原谅而不是许可)更容易受到并发程序中TOC/TOU竞争条件(检查时间到使用时间)的影响。Python中一个相对无害的示例: class Ratio: def __init__(self, numerator, denominator): self.n = numerator self.d = denominator def LBYL_divide(self): if self.d

与LBYL(三思而后行)相比,EAFP(更容易请求原谅而不是许可)更容易受到并发程序中TOC/TOU竞争条件(检查时间到使用时间)的影响。Python中一个相对无害的示例:

class Ratio:
    def __init__(self, numerator, denominator):
        self.n = numerator
        self.d = denominator

    def LBYL_divide(self):
        if self.d != 0:          # check
                                 # another thread can change self.d
            return self.n/self.d # use
        else:
            return "NA"

    def EAFP_divide(self):
        try:
            return self.n/self.d
        except ZeroDivisionError:
            return "NA"
我将这些示例解释为,在EAFP中,没有机会让另一个线程在检查和使用之间溜进,因为它们是在同一行/步骤上完成的。但我的印象是,在抛出异常之前必须检查某些条件;在本例中,
/
必须检查
self.d!=0
知道如何抛出一个
ZeroDivisionError
。为什么在这些检查之后,另一个线程不能潜入并导致问题