Skip to content

Commit 32b79ea

Browse files
authored
Make model score app work on Connect/Shinyapps.io (#657)
1 parent 6d88bb3 commit 32b79ea

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

examples/model-score/scoredata.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,16 @@ async def update_db(position):
5555

5656
def begin():
5757
position = init_db()
58-
asyncio.create_task(update_db(position))
58+
59+
# After initializing the database, we need to start a non-blocking task to update it
60+
# every second or so. If an event loop is already running, we can use an asyncio
61+
# task. (This is the case when running via `shiny run` and shinylive.) Otherwise, we
62+
# need to launch a background thread and run an asyncio event loop there. (This is
63+
# the case when running via shinyapps.io or Posit Connect.)
64+
65+
if asyncio.get_event_loop().is_running():
66+
asyncio.create_task(update_db(position))
67+
else:
68+
from threading import Thread
69+
70+
Thread(target=lambda: asyncio.run(update_db(position)), daemon=True).start()

0 commit comments

Comments
 (0)