Skip to content

Commit c7d65c7

Browse files
authored
gql-cli add signal handlers to catch ctrl-c and close cleanly (#276)
Now the gql-cli script works on Python 3.6 too
1 parent 7eedbbc commit c7d65c7

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

scripts/gql-cli

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
import asyncio
33
import sys
4+
from signal import SIGINT, SIGTERM
45

56
from gql.cli import get_parser, main
67

@@ -9,8 +10,23 @@ parser = get_parser(with_examples=True)
910
args = parser.parse_args()
1011

1112
try:
12-
# Execute the script
13-
exit_code = asyncio.run(main(args))
13+
# Create a new asyncio event loop
14+
loop = asyncio.new_event_loop()
15+
asyncio.set_event_loop(loop)
16+
17+
# Create a gql-cli task with the supplied arguments
18+
main_task = asyncio.ensure_future(main(args), loop=loop)
19+
20+
# Add signal handlers to close gql-cli cleanly on Control-C
21+
for signal in [SIGINT, SIGTERM]:
22+
loop.add_signal_handler(signal, main_task.cancel)
23+
24+
# Run the asyncio loop to execute the task
25+
exit_code = 0
26+
try:
27+
exit_code = loop.run_until_complete(main_task)
28+
finally:
29+
loop.close()
1430

1531
# Return with the correct exit code
1632
sys.exit(exit_code)

0 commit comments

Comments
 (0)