Repository navigation
Conversation
Newer tornados on Python3 use the Python3 asyncio event loop instead of its custom one.
* alarm callbacks fire once and that's all. * idle callbacks fire whenever the event loop has some downtime. They currently re-register themselves to emulate this behaviour * watch_file callbacks fire whenever the file is readable. Implement this by looping and awaiting something to be readable on the fd
|
There is a downside to using The other option would be to signal cancellation using cancel scopes. async def _trio_watch_file(self, fd, callback):
with trio.open_cancel_scope() as cancel_scope:
while True:
await trio.hazmat.wait_readable(fd)
callback()Now this task can be cancelled by calling Though, really, there is a similar problem with the current code as well: it turns out that currently calling I think maybe something like this should resolve these issues? ##### Helper class
class CancelHandle:
def __init__(self):
self._cancelled = False
self._cancel_scope = None
##### inside TrioEventLoop
def __init__(self):
# List of (async_fn, args, handle) tuples
self._queued = []
def _queue_cancellable_operation(self, async_fn, *args):
handle = CancelHandle()
self._queued.append((async_fn, args, handle))
self._maybe_process_queue()
return handle
def _maybe_process_queue(self):
if self._nursery is None:
return
for (async_fn, args, handle) in self._queued:
self._nursery.start_soon(self._run_cancellable_operation, async_fn, args, handle)
self._queued.clear()
async def _run_cancellable_operation(self, async_fn, args, handle):
# In case we were cancelled before we even started
if handle._cancelled:
return
with trio.open_cancel_scope() as cancel_scope:
# In case we're cancelled after we started
handle._cancel_scope = cancel_scope
try:
await async_fn(*args)
finally:
# to make sure future calls to _cancel_operation return False
# and to make sure we don't try doing handle.cancel_scope.cancel() after leaving trio
handle._cancelled = True
def _cancel_operation(self, handle):
if handle._cancelled:
return False
else:
handle._cancelled = True
if handle._cancel_scope is not None:
handle._cancel_scope.cancel()
return TrueThen BTW, I think the current implementation of async def _idle_loop(self, callback):
while True:
await trio.sleep(self._idle_emulation_delay)
callback()
def enter_idle(self, callback):
return self._queue_cancellable_operation(self._idle_loop, callback) |
|
Cool! How to make use of a cancel_scope outside of its context is exactly
the question I was struggling to find a way to solve. I'll give that and
the idle changes a try one my plane lands.
|
|
Some more thoughts:
|
|
FYI, there's now an issue on the trio tracker (python-trio/trio#607) for making cancel scopes natively support being created at a different place than they're used. |
|
@abadger : is this still something you're working on? |
|
Sorry, I've had a lack of time this year, i still have my branch so if
you'd like to close this until i can get more round tuits, that's fine with
me.. (or if you're asking because you'd like to take it over, that's fine
for me too)
|
|
No worries, I was just going through the backlog and this seemed like something that (a) folks might like and (b) seemed reasonably far along. We'll just leave it open for now. |
|
I wish I had found this PR earlier because I have just spent a few hours on implementing a Trio event loop for @abadger, do you plan to revive this PR in the near future? |
|
@ntamas if you'd like to take over and get your changes merged instead, that suits me just fine :-). This has been a tumultuous couple years for me so i haven't had as much time as if like for working on anything in my free time :-) |
|
Okay, I have sent my version of the Trio-based event loop as PR #372. |
|
CLosing in favor of #372 |
Checklist
masterorpython-dual-supportbranchtoxsuccessfully in local environmentDescription:
Trio is an alternative async framework based around the async and await keywords that were added to recent python versions. This PR adds an EventLoop class which uses trio as its backend.
I've marked this as a work in progress as async and await are only available in python-3.6 or greater. I'll have to reorganize the new code so that it is only parsed if we're on python-3.6 or higher. I also want to check whether the eventloop is being tested in tox and try to get a trio developer to look over my work and make sure I'm not making any big mistakes.