Skip to content

Commit d882d66

Browse files
committed
check for running event loop
1 parent 17b3c5e commit d882d66

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

tokencost/constants.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ def refresh_prices(write_file=True):
6262
"""Synchronous wrapper for update_token_costs that optionally writes to model_prices.json."""
6363
import asyncio
6464
try:
65-
# Run the async function in a new event loop
66-
updated_costs = asyncio.run(update_token_costs())
65+
# Check if an event loop is already running
66+
try:
67+
loop = asyncio.get_running_loop()
68+
updated_costs = loop.run_until_complete(update_token_costs())
69+
except RuntimeError:
70+
# No running loop, safe to use asyncio.run
71+
updated_costs = asyncio.run(update_token_costs())
6772

6873
# Write to file if requested
6974
if write_file:

0 commit comments

Comments
 (0)