Asynchronous
Lv 1 廚房菜鳥:(Single thread)
Lv 2 廚房進階菜鳥:(Asynchronous)
Lv 3 廚房老手:(Multiple threads & Asynchronous)
一般模式(非使用Async):
import time
def brew_coffee():
print("Start brew coffee")
time.sleep(3 * 60)
print("End brew coffee")
return "coffee ready"
def toast_bagel():
print("Start toast bagel")
time.sleep(5 * 60)
print("End toast bagel")
return "bagel toasted"
def main():
start_time = time.time()
result_coffee = brew_coffee()
result_bagel = toast_bagel()
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Result of brew_coffee: {result_coffee}")
print(f"Result of toast bagel: {result_bagel}")
print(f"Total execution time: {elapsed_time:2f} seconds")
if __name__ == "__main__":
main()Async模式
Reference
Last updated