Hello!
I've been experimenting with using twisted in an ipython notebook recently, and I noticed that ipython has support for implicitly awaiting asyncio coroutines but that twisted support wasn't included.
I did a bit of research into what it might take to do it, which you can see in this gist here:
https://gist.github.com/jfhbrook/29d4076bcf43ce64a462ea7911b59393
So it seems like I have an implementation here that almost works. I think the issue I'm having is that the async runner attempts to create a new event loop, even though it's already running. I'm not 100% sure that this is the right approach, but it does seem like these magics are intended to run "synchronously" - perhaps support for awaitable methods on magics is what's needed here?
Twisted also has an added challenge in that it needs to explicitly be integrated into the event loop, which I don't think any of the other async runners have to solve for. I guess I'd be looking for advice here in terms of whether to require the user to add that integration manually or whether we would want to add some kind of optional attempted install.
Beyond these questions I think I see the general direction this would have to go, and I'd be interested in putting this together in a more official way if there's interest (and if we can figure out how to solve the problems I ran into).
Update:
I was able to solve these problems in the linked notebook by not integrating the twisted reactor and using a trick similar to twisted.internet.task#react. Given the way autoawait is currently structured I think this is the right approach, minus edge cases that react might be handling that I don't see, and minus, uh, the fact that twisted doesn't actually let you restart the reactor. 🙃
Update 2:
Using a similar strategy but with a library suggested by #twisted on irc called crochet seems to solve a lot of these problems! But I'm not sure how people would feel about an extra library.
Thanks!
Hello!
I've been experimenting with using twisted in an ipython notebook recently, and I noticed that ipython has support for implicitly awaiting asyncio coroutines but that twisted support wasn't included.
I did a bit of research into what it might take to do it, which you can see in this gist here:
https://gist.github.com/jfhbrook/29d4076bcf43ce64a462ea7911b59393
So it seems like I have an implementation here that almost works. I think the issue I'm having is that the async runner attempts to create a new event loop, even though it's already running. I'm not 100% sure that this is the right approach, but it does seem like these magics are intended to run "synchronously" - perhaps support for awaitable methods on magics is what's needed here?
Twisted also has an added challenge in that it needs to explicitly be integrated into the event loop, which I don't think any of the other async runners have to solve for. I guess I'd be looking for advice here in terms of whether to require the user to add that integration manually or whether we would want to add some kind of optional attempted install.
Beyond these questions I think I see the general direction this would have to go, and I'd be interested in putting this together in a more official way if there's interest (and if we can figure out how to solve the problems I ran into).
Update:
I was able to solve these problems in the linked notebook by not integrating the twisted reactor and using a trick similar to twisted.internet.task#react. Given the way autoawait is currently structured I think this is the right approach, minus edge cases that react might be handling that I don't see, and minus, uh, the fact that twisted doesn't actually let you restart the reactor. 🙃
Update 2:
Using a similar strategy but with a library suggested by #twisted on irc called crochet seems to solve a lot of these problems! But I'm not sure how people would feel about an extra library.
Thanks!