Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trinity handling of KeyboardInterrupt #385

Closed
pipermerriam opened this issue Feb 21, 2018 · 1 comment
Closed

Trinity handling of KeyboardInterrupt #385

pipermerriam opened this issue Feb 21, 2018 · 1 comment

Comments

@pipermerriam
Copy link
Member

What is wrong?

Currently, trinity is not correctly handling KeyboardInterrupt for asyncio loop based processes.

How can it be fixed

Fill this in if you know how to fix it.

We need to make use of the asyncio.loop.add_signal_handler API to do proper keyboard interrupt signal handling.

@namratachaudhary
Copy link

asyncio is very low-level

For KeyboardInterrupt, there are varied behaviours. Check this issue : python/asyncio#341

It depends on how you trigger KeyboardInterrupt. When there is a callback that doesn't return - you need to use the signal to break through that, eg a loop like

while True: 
    pass

Adding asyncio.loop.add_signal_handler might not work because it wouldn't stop such a loop.

Otoh, if there's an event loop or a queue running, and you do not want the signal to interrupt that routine, you can use asyncio.loop.add_signal_handler .

Another way to do it (naively), would be :

loop = asyncio.get_event_loop()

tasks = asyncio.gather(
        asyncio.async(some_task(second=5)),
        asyncio.async(some_task(seconds=10))
    )

try:
    loop.run_until_complete(tasks)
except KeyboardInterrupt as e:
    print("Caught keyboard interrupt..")
    tasks.cancel()
    loop.run_forever()
    tasks.exception()
finally:
    loop.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants