print()在异常后输出到控制台,延迟记录顺序

时间:2017-08-07 15:39:45

标签: python-3.x python-unittest

我有以下验证程序:

def validate_schema(self, name, data, schema_filename):
    Logging.message("validating {0} against schema".format(name))
    with open(schema_filename) as f:
        schema = json.load(f)
        resolver = jsonschema.RefResolver(
            "file:///{0}/".format(os.path.abspath(schema_filename.rsplit('/', 1)[0])), schema
        )
        jsonschema.validate(data, schema, resolver=resolver)
        Logging.ok()

类日志记录包含:

@staticmethod
def ok():
    print("OK")

@staticmethod
def message(msg):
    print("> " + msg, end='... ')

当架构验证失败时,我首先获得异常文本,并且"验证..."跟随它的消息。

为什么会发生这种情况以及如何保持输出顺序?

1 个答案:

答案 0 :(得分:1)

刷新stdout可以解决问题

@staticmethod
def message(text):
    print("> " + text, end='... ')
    sys.stdout.flush()