0

I've got code that looks somewhat like:

@asyncio.coroutine
def myfunction():
    result = yield from bot.whois(something)  # returns an asyncio.Future()
    return result['account']

How would I convert this to something that works in Python 2.7? Is that even possible?

The question Converting "yield from" statement to Python 2.7 code isn't applicable, because it throws away the result.

Community
  • 1
  • 1
Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
  • 1
    Preliminary research shows that this might not be possible without resorting to callbacks or using a Trollius event loop instead of Asyncio https://trollius.readthedocs.org/asyncio.html#differences-between-trollius-and-tulip – Thom Wiggers Jul 27 '15 at 22:57
  • `for result in bot.whois(something): yield result` didn't work? I've also never seen a function with both `yield` and `return`. – TigerhawkT3 Jul 27 '15 at 22:58
  • No, it doesn't. `yield from` is quite different. https://docs.python.org/3/library/asyncio-task.html – Thom Wiggers Jul 27 '15 at 23:00
  • pep380 specifies an equivalence between yield from and code that returns stuff - however it is quite complex. https://www.python.org/dev/peps/pep-0380/#id13 – Thom Wiggers Jul 27 '15 at 23:03
  • `asyncio` isn't compatible with Python 2.x. As you mentioned, you need to use trollius instead. – dano Jul 28 '15 at 01:09
  • `yield from` isn't limited to asyncio though.... – Thom Wiggers Jul 28 '15 at 07:57
  • Have you looked at implementing something similar in twisted? – Jared Mackey Aug 21 '15 at 15:19
  • Switching frameworks would mean tossing out everything I've already got, and that's not really an option for me. I've just tossed Python <3.3 compatibility. – Thom Wiggers Aug 21 '15 at 15:48

0 Answers0