rsalmei / alive-progress

A new kind of Progress Bar, with real-time throughput, ETA, and very cool animations!
MIT License
5.53k stars 206 forks source link

with asyncio still print still after ctrl-c in ipython #205

Closed NewUserHa closed 1 year ago

NewUserHa commented 1 year ago
import asyncio
from alive_progress import alive_bar

letters = [chr(ord('A') + x) for x in range(26)]
with alive_bar(26, dual_line=True, title='Alphabet') as bar:
    for c in letters:
        bar.text = f'-> Teaching the letter: {c}, please wait...'
        if c in 'HKWZ':
            print(f'fail "{c}", retry later')
        await asyncio.sleep(0.3)
        bar()
NewUserHa commented 1 year ago

may relate to https://github.com/ipython/ipython/issues/13850

rsalmei commented 1 year ago

Interesting... I can reproduce it, but I'm not sure why this happens. I know internally I start a thread for the rendering work, but I've included a try/finally that stops it!!! It does seem related to ipython, because as far as I know, an async code must be run inside an async loop, and since this code doesn't start one, ipython must be doing it. Actually, the CTRL+C stack trace does show a run_until_complete call:

on 5: ---------------------------------------------------------------------------
      KeyboardInterrupt                         Traceback (most recent call last)
      ~/.pyenv/versions/3.7.12/envs/alive37/lib/python3.7/site-packages/IPython/core/async_helpers.py in __call__(self, coro)
           36         Handler for asyncio autoawait
           37         """
      ---> 38         return self.loop.run_until_complete(coro)
           39
           40     def __str__(self):

      ~/.pyenv/versions/3.7.12/lib/python3.7/asyncio/base_events.py in run_until_complete(self, future)
          572         future.add_done_callback(_run_until_complete_cb)
          573         try:
      --> 574             self.run_forever()
          575         except:
          576             if new_task and future.done() and not future.cancelled():

      ~/.pyenv/versions/3.7.12/lib/python3.7/asyncio/base_events.py in run_forever(self)
          539             events._set_running_loop(self)
          540             while True:
      --> 541                 self._run_once()
          542                 if self._stopping:
          543                     break

      ~/.pyenv/versions/3.7.12/lib/python3.7/asyncio/base_events.py in _run_once(self)
         1748                            timeout * 1e3, dt * 1e3)
         1749         else:
      -> 1750             event_list = self._selector.select(timeout)
         1751         self._process_events(event_list)
         1752

      ~/.pyenv/versions/3.7.12/lib/python3.7/selectors.py in select(self, timeout)
          556             ready = []
          557             try:
      --> 558                 kev_list = self._selector.control(None, max_ev, timeout)
          559             except InterruptedError:
          560                 return ready

      KeyboardInterrupt:
rsalmei commented 1 year ago

I'm closing this one because unfortunately, I can't do anything about it.