async_delayed_execution.py
· 398 B · Python
原始檔案
import threading
import time
def delayed_execution():
# 在這裡執行延遲後的任務
print("Delayed execution")
def async_sleep(seconds):
def sleep():
time.sleep(seconds)
delayed_execution()
thread = threading.Thread(target=sleep)
thread.start()
# 呼叫非同步的 sleep 函數
async_sleep(1)
# 繼續執行其他任務
print("Continuing execution")
| 1 | import threading |
| 2 | import time |
| 3 | |
| 4 | def delayed_execution(): |
| 5 | # 在這裡執行延遲後的任務 |
| 6 | print("Delayed execution") |
| 7 | |
| 8 | def async_sleep(seconds): |
| 9 | def sleep(): |
| 10 | time.sleep(seconds) |
| 11 | delayed_execution() |
| 12 | |
| 13 | thread = threading.Thread(target=sleep) |
| 14 | thread.start() |
| 15 | |
| 16 | # 呼叫非同步的 sleep 函數 |
| 17 | async_sleep(1) |
| 18 | |
| 19 | # 繼續執行其他任務 |
| 20 | print("Continuing execution") |
| 21 |