#๐Ÿ”’ Asyncio confusion

13 messages ยท Page 1 of 1 (latest)

random knot
#

I'm currently working on my first project in Python. My main problem is getting a function to run in parallel without stopping the rest of the program.

import asyncio
from winsdk.windows.media.control import GlobalSystemMediaTransportControlsSessionManager as MediaManager

async def init():

    manager = await MediaManager.request_async()
    global media_changed_token
    media_changed_token = manager.get_current_session().add_media_properties_changed(handle_media_changed)

    asyncio.create_task(media_changed())
    event = asyncio.Event()
    await event.wait()```
This `init()` function is run once as part of the entire program. The goal is to run the `handle_media_changed` function every time the media Windows is currently playing changes (using `winsdk`, which seems pretty undocumented).

This works, except the `await event.wait()` stops the rest of the program from running, as there are things that should happen after the `init` function in the main loop, but removing the `await`ing prevents the `handle_media_changed` function from firing at all.

My basic understanding of asyncio doesn't really allow me to find the "right" way/alternative to do this. Any help is appreciated. ๐Ÿ˜…
steep sedgeBOT
#

@random knot

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

random knot
#

I'm mostly quite confused as to what the

event = asyncio.Event()
await event.wait()```lines could possibly change to the way the media property event fires. It works exclusively when these two lines are present or when another await argument for another function completely is fired, which makes me wonder how to make the code "wait" for this event in parallel of running the rest of its code.
inner gulch
#

What you might be facing are issues with gc in python since manager will get deleted after that function is fully executed.

#

You can try declaring manager global in init function to test if it is indeed getting deleted by gc

random knot
#

I've turned manager into a global variable, but the function still doesn't seem to fire. It only seems to fire when the code is in the state of waiting for this event that never happens, which is quite confusing. :/

#

However, I think I get what you mean by the manager being deleted, I'll keep poking in that direction.

#

Alright, my apologies. Your solution absolutely worked.

#

Thank you so much!!

#

!close

steep sedgeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.