Skip to content

Documentation revision: AttributeError: 'DummyTqdmFile' object has no attribute 'flush' #439

@MPagel

Description

@MPagel

Following https://pypi.python.org/pypi/tqdm#redirecting-writing, I get AttributeError: 'DummyTqdmFile' object has no attribute 'flush' if trying to redirect output to sys.stdout/stderr through tqdm first.

This error fired due to an interaction with the eventlet package where that package calls sys.stderr.flush()

Traceback (most recent call last):
File "ReadQueues.py", line 1286, in
main(sys.argv)
File "ReadQueues.py", line 1243, in main
if not SkipDownload: processAllQueues(cfgglob)
File "ReadQueues.py", line 1093, in processAllQueues
for (que_data,spec_section,bar) in SQSpool.imap(doQueueDownload,arSects):
File "C:\Program Files\Python\27\lib\site-packages\eventlet\greenpool.py", line 244, in next
val = self.waiters.get().wait()
File "C:\Program Files\Python\27\lib\site-packages\eventlet\greenthread.py", line 179, in wait
return self._exit_event.wait()
File "C:\Program Files\Python\27\lib\site-packages\eventlet\event.py", line 121, in wait
return hubs.get_hub().switch()
File "C:\Program Files\Python\27\lib\site-packages\eventlet\hubs\hub.py", line 295, in switch
return self.greenlet.switch()
File "C:\Program Files\Python\27\lib\site-packages\eventlet\hubs\hub.py", line 337, in run
self.fire_timers(self.clock())
File "C:\Program Files\Python\27\lib\site-packages\eventlet\hubs\hub.py", line 462, in fire_timers
self.squelch_timer_exception(timer, sys.exc_info())
File "C:\Program Files\Python\27\lib\site-packages\eventlet\hubs\hub.py", line 387, in squelch_timer_exception
sys.stderr.flush()
AttributeError: 'DummyTqdmFile' object has no attribute 'flush'
I've now added

    def flush(self):
        fp_flush = getattr(self.file, 'flush', lambda: None)
        fp_flush()

to the DummyTqdmFile class in my code.
Full class def is now

  from tqdm import tqdm
  import contextlib
  class DummyTqdmFile(object):
    """Dummy file-like that will write to tqdm"""
    file = None
    def __init__(self, file):
        self.file = file
    def flush(self):
        fp_flush = getattr(self.file, 'flush', lambda: None)
        fp_flush()
    def write(self, x):
        # Avoid print() second call (useless \n)
        if len(x.rstrip()) > 0:
            tqdm.write(x, file=self.file)

If that works, I suggest you revise the documentation at tqdm#redirecting-writing accordingly

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions