import asyncio import threading from fastapi import APIRouter router = APIRouter(prefix="/Invoice") def _run_fetch(): from invoice_fetcher import main as fetch_main loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: loop.run_until_complete(fetch_main()) finally: loop.close() @router.get("/fetch") async def fetch_invoices(): print("🚀 開始抓取發票...") threading.Thread(target=_run_fetch).start() return {"status": "started"}