""" 首先梳理一下場景 有個發郵件需求 請求某個接口 接口返回成功之后 2后 發送郵件提醒 前提接口流程必須走完 有正確返回 項目中沒有其他異步框架的時候 可以使用下邊方式 做個簡單的異步 1.寫好異步裝飾器, 2.將需要延遲的操作寫到函數中 將裝飾器 放到函數頭頂 3.正常走接口流程 代碼不會堵塞 4.不說原理了(感覺有點low 不過有時可以解決問題) """
from threading import Thread from time import sleep def async(f): def wrapper(*args, **kwargs): thr = Thread(target=f, args=args, kwargs=kwargs) thr.start() return wrapper @async def get_ddos_status(account_name, customer_id, type_, flag, start): time.sleep(10) service.send_mail_(account_name, customer_id, type_, flag, start)