Skip to content

Commit

Permalink
Merge pull request #36 from superleesa/main
Browse files Browse the repository at this point in the history
Support running toast function when there is an existing event loop
  • Loading branch information
GitHub30 authored Mar 4, 2024
2 parents f93bbd7 + 18ef9ed commit c50c019
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion win11toast.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,26 @@ async def toast_async(title=None, body=None, on_click=print, icon=None, image=No


def toast(*args, **kwargs):
return asyncio.run(toast_async(*args, **kwargs))
toast_coroutine = toast_async(*args, **kwargs)

# check if there is an existing loop
try:
loop = asyncio.get_running_loop()
except RuntimeError:
return asyncio.run(toast_coroutine)
else:
future = asyncio.Future()
task = loop.create_task(toast_coroutine)

def on_done(t):
if t.exception() is not None:
future.set_exception(t.exception())
else:
future.set_result(t.result())

task.add_done_callback(on_done)
return future



def update_progress(progress, app_id=DEFAULT_APP_ID):
Expand Down

0 comments on commit c50c019

Please sign in to comment.