一个同时支持异步和同步的Python函数
Scenario: I often need to write Python functions like: take some parameters and format them call an API with the formatted parameters parse the result and return chosen values There's a huge...
本文探讨了如何编写一个同时支持异步和同步调用的Python函数。由于async/await的“传染性”,通常需要分别实现同步和异步版本,导致代码重复。作者提出通过装饰器将函数转换为可同时支持异步和同步调用的形式,从而简化代码结构,提高可维护性。