Keyboard Interrupting an asyncio.run() raises CancelledError and keeps running the code
up vote
0
down vote
favorite
I have inspected this SO question on how to gracefully close out the asyncio process. Although, when I perform it on my code:
async def ob_main(product_id: str, freq: int) -> None:
assert freq >= 1, f'The minimum frequency is 1s. Adjust your value: {freq}.'
save_loc = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'ob', product_id)
while True:
close = False
try:
full_save_path = create_save_location(save_loc)
file = open(full_save_path, 'a', encoding='utf-8')
await ob_collector(product_id, file)
await asyncio.sleep(freq)
except KeyboardInterrupt:
close = True
task.cancel()
loop.run_forever()
task.exception()
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
error_msg = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
print(error_msg)
logger.warning(f'[1]-Error encountered collecting ob data: {error_msg}')
finally:
if close:
loop.close()
cwow()
exit(0)
I get the following traceback printed in terminal:
^C['Traceback (most recent call last):n', ' File "/anaconda3/lib/python3.7/asyncio/runners.py", line 43, in runn return loop.run_until_complete(main)n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 555, in run_until_completen self.run_forever()n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 523, in run_forevern self._run_once()n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 1722, in _run_oncen event_list = self._selector.select(timeout)n', ' File "/anaconda3/lib/python3.7/selectors.py", line 558, in selectn kev_list = self._selector.control(None, max_ev, timeout)n', 'KeyboardInterruptn', 'nDuring handling of the above exception, another exception occurred:nn', 'Traceback (most recent call last):n', ' File "coinbase-collector.py", line 98, in ob_mainn await asyncio.sleep(freq)n', ' File "/anaconda3/lib/python3.7/asyncio/tasks.py", line 564, in sleepn return await futuren', 'concurrent.futures._base.CancelledErrorn']
and the code keeps running.
task
and loop
are the variables from the global scope, defined in the __main__
:
loop = asyncio.get_event_loop()
task = asyncio.run(ob_main(args.p, 10))
python-3.x python-asyncio
add a comment |
up vote
0
down vote
favorite
I have inspected this SO question on how to gracefully close out the asyncio process. Although, when I perform it on my code:
async def ob_main(product_id: str, freq: int) -> None:
assert freq >= 1, f'The minimum frequency is 1s. Adjust your value: {freq}.'
save_loc = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'ob', product_id)
while True:
close = False
try:
full_save_path = create_save_location(save_loc)
file = open(full_save_path, 'a', encoding='utf-8')
await ob_collector(product_id, file)
await asyncio.sleep(freq)
except KeyboardInterrupt:
close = True
task.cancel()
loop.run_forever()
task.exception()
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
error_msg = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
print(error_msg)
logger.warning(f'[1]-Error encountered collecting ob data: {error_msg}')
finally:
if close:
loop.close()
cwow()
exit(0)
I get the following traceback printed in terminal:
^C['Traceback (most recent call last):n', ' File "/anaconda3/lib/python3.7/asyncio/runners.py", line 43, in runn return loop.run_until_complete(main)n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 555, in run_until_completen self.run_forever()n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 523, in run_forevern self._run_once()n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 1722, in _run_oncen event_list = self._selector.select(timeout)n', ' File "/anaconda3/lib/python3.7/selectors.py", line 558, in selectn kev_list = self._selector.control(None, max_ev, timeout)n', 'KeyboardInterruptn', 'nDuring handling of the above exception, another exception occurred:nn', 'Traceback (most recent call last):n', ' File "coinbase-collector.py", line 98, in ob_mainn await asyncio.sleep(freq)n', ' File "/anaconda3/lib/python3.7/asyncio/tasks.py", line 564, in sleepn return await futuren', 'concurrent.futures._base.CancelledErrorn']
and the code keeps running.
task
and loop
are the variables from the global scope, defined in the __main__
:
loop = asyncio.get_event_loop()
task = asyncio.run(ob_main(args.p, 10))
python-3.x python-asyncio
What isloop.run_forever()
in the exception handler forKeyboardInterrupt
supposed to achieve? It looks like a possible source of the problem.
– user4815162342
Nov 22 at 17:27
I do not even have theloop.run_forever()
anywhere, actually
– i squared - Keep it Real
Nov 22 at 17:35
Can you then post your actual code? The code in the question hasloop.run_forever
on line 16 or so.
– user4815162342
Nov 22 at 18:12
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have inspected this SO question on how to gracefully close out the asyncio process. Although, when I perform it on my code:
async def ob_main(product_id: str, freq: int) -> None:
assert freq >= 1, f'The minimum frequency is 1s. Adjust your value: {freq}.'
save_loc = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'ob', product_id)
while True:
close = False
try:
full_save_path = create_save_location(save_loc)
file = open(full_save_path, 'a', encoding='utf-8')
await ob_collector(product_id, file)
await asyncio.sleep(freq)
except KeyboardInterrupt:
close = True
task.cancel()
loop.run_forever()
task.exception()
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
error_msg = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
print(error_msg)
logger.warning(f'[1]-Error encountered collecting ob data: {error_msg}')
finally:
if close:
loop.close()
cwow()
exit(0)
I get the following traceback printed in terminal:
^C['Traceback (most recent call last):n', ' File "/anaconda3/lib/python3.7/asyncio/runners.py", line 43, in runn return loop.run_until_complete(main)n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 555, in run_until_completen self.run_forever()n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 523, in run_forevern self._run_once()n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 1722, in _run_oncen event_list = self._selector.select(timeout)n', ' File "/anaconda3/lib/python3.7/selectors.py", line 558, in selectn kev_list = self._selector.control(None, max_ev, timeout)n', 'KeyboardInterruptn', 'nDuring handling of the above exception, another exception occurred:nn', 'Traceback (most recent call last):n', ' File "coinbase-collector.py", line 98, in ob_mainn await asyncio.sleep(freq)n', ' File "/anaconda3/lib/python3.7/asyncio/tasks.py", line 564, in sleepn return await futuren', 'concurrent.futures._base.CancelledErrorn']
and the code keeps running.
task
and loop
are the variables from the global scope, defined in the __main__
:
loop = asyncio.get_event_loop()
task = asyncio.run(ob_main(args.p, 10))
python-3.x python-asyncio
I have inspected this SO question on how to gracefully close out the asyncio process. Although, when I perform it on my code:
async def ob_main(product_id: str, freq: int) -> None:
assert freq >= 1, f'The minimum frequency is 1s. Adjust your value: {freq}.'
save_loc = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'ob', product_id)
while True:
close = False
try:
full_save_path = create_save_location(save_loc)
file = open(full_save_path, 'a', encoding='utf-8')
await ob_collector(product_id, file)
await asyncio.sleep(freq)
except KeyboardInterrupt:
close = True
task.cancel()
loop.run_forever()
task.exception()
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
error_msg = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
print(error_msg)
logger.warning(f'[1]-Error encountered collecting ob data: {error_msg}')
finally:
if close:
loop.close()
cwow()
exit(0)
I get the following traceback printed in terminal:
^C['Traceback (most recent call last):n', ' File "/anaconda3/lib/python3.7/asyncio/runners.py", line 43, in runn return loop.run_until_complete(main)n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 555, in run_until_completen self.run_forever()n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 523, in run_forevern self._run_once()n', ' File "/anaconda3/lib/python3.7/asyncio/base_events.py", line 1722, in _run_oncen event_list = self._selector.select(timeout)n', ' File "/anaconda3/lib/python3.7/selectors.py", line 558, in selectn kev_list = self._selector.control(None, max_ev, timeout)n', 'KeyboardInterruptn', 'nDuring handling of the above exception, another exception occurred:nn', 'Traceback (most recent call last):n', ' File "coinbase-collector.py", line 98, in ob_mainn await asyncio.sleep(freq)n', ' File "/anaconda3/lib/python3.7/asyncio/tasks.py", line 564, in sleepn return await futuren', 'concurrent.futures._base.CancelledErrorn']
and the code keeps running.
task
and loop
are the variables from the global scope, defined in the __main__
:
loop = asyncio.get_event_loop()
task = asyncio.run(ob_main(args.p, 10))
python-3.x python-asyncio
python-3.x python-asyncio
asked Nov 22 at 15:59
i squared - Keep it Real
697519
697519
What isloop.run_forever()
in the exception handler forKeyboardInterrupt
supposed to achieve? It looks like a possible source of the problem.
– user4815162342
Nov 22 at 17:27
I do not even have theloop.run_forever()
anywhere, actually
– i squared - Keep it Real
Nov 22 at 17:35
Can you then post your actual code? The code in the question hasloop.run_forever
on line 16 or so.
– user4815162342
Nov 22 at 18:12
add a comment |
What isloop.run_forever()
in the exception handler forKeyboardInterrupt
supposed to achieve? It looks like a possible source of the problem.
– user4815162342
Nov 22 at 17:27
I do not even have theloop.run_forever()
anywhere, actually
– i squared - Keep it Real
Nov 22 at 17:35
Can you then post your actual code? The code in the question hasloop.run_forever
on line 16 or so.
– user4815162342
Nov 22 at 18:12
What is
loop.run_forever()
in the exception handler for KeyboardInterrupt
supposed to achieve? It looks like a possible source of the problem.– user4815162342
Nov 22 at 17:27
What is
loop.run_forever()
in the exception handler for KeyboardInterrupt
supposed to achieve? It looks like a possible source of the problem.– user4815162342
Nov 22 at 17:27
I do not even have the
loop.run_forever()
anywhere, actually– i squared - Keep it Real
Nov 22 at 17:35
I do not even have the
loop.run_forever()
anywhere, actually– i squared - Keep it Real
Nov 22 at 17:35
Can you then post your actual code? The code in the question has
loop.run_forever
on line 16 or so.– user4815162342
Nov 22 at 18:12
Can you then post your actual code? The code in the question has
loop.run_forever
on line 16 or so.– user4815162342
Nov 22 at 18:12
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Applying this question's method solves the issue. So in the above case:
try:
loop.run_until_complete(ob_main(args.p, 10))
except KeyboardInterrupt:
cwow()
exit(0)
However, I do not uderstand why that works.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Applying this question's method solves the issue. So in the above case:
try:
loop.run_until_complete(ob_main(args.p, 10))
except KeyboardInterrupt:
cwow()
exit(0)
However, I do not uderstand why that works.
add a comment |
up vote
0
down vote
Applying this question's method solves the issue. So in the above case:
try:
loop.run_until_complete(ob_main(args.p, 10))
except KeyboardInterrupt:
cwow()
exit(0)
However, I do not uderstand why that works.
add a comment |
up vote
0
down vote
up vote
0
down vote
Applying this question's method solves the issue. So in the above case:
try:
loop.run_until_complete(ob_main(args.p, 10))
except KeyboardInterrupt:
cwow()
exit(0)
However, I do not uderstand why that works.
Applying this question's method solves the issue. So in the above case:
try:
loop.run_until_complete(ob_main(args.p, 10))
except KeyboardInterrupt:
cwow()
exit(0)
However, I do not uderstand why that works.
answered Nov 22 at 16:37
i squared - Keep it Real
697519
697519
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434605%2fkeyboard-interrupting-an-asyncio-run-raises-cancellederror-and-keeps-running-t%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is
loop.run_forever()
in the exception handler forKeyboardInterrupt
supposed to achieve? It looks like a possible source of the problem.– user4815162342
Nov 22 at 17:27
I do not even have the
loop.run_forever()
anywhere, actually– i squared - Keep it Real
Nov 22 at 17:35
Can you then post your actual code? The code in the question has
loop.run_forever
on line 16 or so.– user4815162342
Nov 22 at 18:12