Skip to content

Commit

Permalink
Fixed async list dir
Browse files Browse the repository at this point in the history
  • Loading branch information
berrysauce committed Oct 11, 2024
1 parent 226954a commit d7edd8b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions ingredients.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ async def scan(url):
def add_ingredient(category: str, ingredient: str):
if f"{category}/{ingredient}" not in matching_ingredients:
matching_ingredients.append(f"{category}/{ingredient}")

async def listdir_async(path):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, os.listdir, path)

categories = await aiofiles.os.listdir("ingredients")
categories = await listdir_async("ingredients")

if "categories.json" in categories:
categories.remove("categories.json")
Expand All @@ -41,7 +45,7 @@ def add_ingredient(category: str, ingredient: str):
# ----------------------------------------

for category in categories:
ingredients = await aiofiles.os.listdir(f"ingredients/{category}")
ingredients = await listdir_async(f"ingredients/{category}")

if ".DS_Store" in ingredients:
ingredients.remove(".DS_Store") # macOS only, improves compatibility
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def get_scan(url: str, includeCategories: Optional[bool] = False):
except httpx.RequestError as e:
raise HTTPException(status_code=400, detail=str(e))
except:
raise HTTPException(status_code=500, detail=f"Unknown error")
raise HTTPException(status_code=500, detail=f"Server error")


@app.get("/icon/{icon}")
Expand Down

0 comments on commit d7edd8b

Please sign in to comment.