Hi,

I just joined this mailing list, but not overly sure if it’s the right
place for this discussion or now - please tell me to quite down and go away
if so :). But if you’d be so kind as to point me in the right direction
that would also be good.

I’m currently building a library which has an asynchronous interface and I
wanted to make it also be callable serially - handy for users of the
library not using coroutines, as well as just using in the REPL. I didn’t
want to maintain two implementations of essentially the same functions (one
with awaits and ones without); I just wanted to run the async function on
pythons current loop. I also wanted to present the interface in a
consistent way without the need to prefix or suffix ‘async’ into the
function name. e.g.

result = my_function(asynchronous=False)
result = await my_function(asynchronous=True)

I didn’t think this would be possible but I think I managed it. I just want
to sanity check this because I think it’s pretty cool if it works.  I’ve
attached the code I wrote to test it. The assumption I’ve made is
about get_event_loop making sense for just picking the current python
thread of execution. Any thoughts?

If attachments aren’t supported, here’s a gist:
https://gist.github.com/JamesStidard/3317969472f4b3fc938a4ab29c5e1ecf

Thanks,
James

Attachment: dual_interface.py
Description: Binary data

_______________________________________________
Async-sig mailing list
Async-sig@python.org
https://mail.python.org/mailman/listinfo/async-sig
Code of Conduct: https://www.python.org/psf/codeofconduct/

Reply via email to