You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to connect a signal to an async callback, but it doesn't seem to be supported.
What I Did
This gives a "coroutine 'on_value_changed' was never awaited" warning:
frompsygnalimportSignal# define an object with class attribute SignalsclassMyObj:
# this signal will emit a single stringvalue_changed=Signal(str)
def__init__(self, value=0):
self._value=valuedefset_value(self, value):
ifvalue!=self._value:
self._value=str(value)
# emit the signalself.value_changed.emit(self._value)
asyncdefon_value_changed(new_value: str):
print(f"The new value is {new_value!r}")
# instantiate the object with Signalsobj=MyObj()
# connect one or more callbacks with `connect`obj.value_changed.connect(on_value_changed)
# callbacks are called when value changesobj.set_value('hello!') # prints: 'The new value is 'hello!'
The text was updated successfully, but these errors were encountered:
you're right, we haven't explicitly supported coroutine functions yet. #346 should handle this case, but I'll note a slight modification to what you'll need to do in your example: Because psygnal itself has no concept of an event loop (it's a "bring-your-own-event-loop" library), you would need to slightly modify your example to call obj.set_value() within the context of an asyncio event loop. So, basically just change that last line with:
asyncdefmain():
# callbacks are called when value changesobj.set_value("hello!")
importasyncioasyncio.run(main())
... and then with #346, it should work. Still need to clean up some typing. But if you have a moment to look and if you have any comments or suggestions, i'd be happy to hear them
Description
I'd like to connect a signal to an async callback, but it doesn't seem to be supported.
What I Did
This gives a
"coroutine 'on_value_changed' was never awaited"
warning:The text was updated successfully, but these errors were encountered: