schollii / pypubsub

A Python publish-subcribe library (moved here from SourceForge.net where I had it for many years)
189 stars 29 forks source link

Async support #46

Open makkus opened 4 years ago

makkus commented 4 years ago

I haven't looked too deeply in what would be involved to implement this properly, but before I do I was wondering whether you have any thoughts on this.

The main thing I'd like is to be able to provide a listener callback that is a co-routine instead of a 'normal' function. My current workaround is to convert any 'await'-able calls inside my listener to sync, but I thought it might be nicer if pypubsub could handle async callbacks natively.

As a first step, it might be good enough to check if the listener is a co-routine, and convert/await it inside the relevant pypubsub-component. Might be opening up a big can of multi-thread-worms and not worth it, not sure.

Ideally of course, pypubsub would handle async stuff natively, but that is most likely too much to ask, and probably better to look at something like https://github.com/abadger/pubmarine , but overall I'm very happy with pypubsub and it fits most of my requirments perfectly.

teharris1 commented 4 years ago

Up vote for the feature!

teharris1 commented 4 years ago

Perhaps an easy entry point into the ability to run an async listener is to test if the asyncio loop is running. If it is running, await the listener. If it is not running throw an exception. This would put the responsibility of the loop on the consumer of the module.

schollii commented 3 years ago

Best way to handle this is to try it out: create some code that exemplifies how you want to use it with co-routines, and then try to path pypubsub so it works the way you want it with those, without impacting existing functionality. Then you can submit a pull request. If some changes are needed to existing functionality, discuss it here first, before spending too much time, as there is a chance it will not be acceptable, but it also might be, or maybe there will be different approach once we discuss the reason.