Skip to content

IPython & PyCharm debugging mode: I/O operation on closed file #202

Description

@pauljherrera

Eighty percent of the time I use tqdm it works perfectly. The other 20% I get 'I/O operation on closed file'.

The weird thing about it is that I can use tqdm in the same script two times. The first time it doesn't work. The second time, without changing anything , it works.

I paste below the complete traceback:

File "<ipython-input-160-51605e3202c6>", line 1, in <module>
    runfile('C:/Users/forex/Documents/Knoxpy simple/Avanti_Analyzer.py', wdir='C:/Users/forex/Documents/Knoxpy simple')

  File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/forex/Documents/Knoxpy simple/Avanti_Analyzer.py", line 1114, in <module>
    barsperday, commission, swap, join, verbose = True)

  File "C:/Users/forex/Documents/Knoxpy simple/Avanti_Analyzer.py", line 1022, in w_eventstudy
    df = overlaps(df, overlapperiod, signal, trigger)

  File "C:/Users/forex/Documents/Knoxpy simple/Avanti_Analyzer.py", line 438, in overlaps
    for i in tqdm(xrange(overlapperiod - 1, len(x1))):

  File "C:\Anaconda2\lib\site-packages\tqdm\_tqdm.py", line 622, in __iter__
    else ncols),

  File "C:\Anaconda2\lib\site-packages\tqdm\_tqdm.py", line 98, in print_status
    fp_write('\r' + s + (' ' * max(last_len[0] - len_s, 0)))

  File "C:\Anaconda2\lib\site-packages\tqdm\_tqdm.py", line 92, in fp_write
    fp_flush()

  File "C:\Anaconda2\lib\site-packages\colorama\ansitowin32.py", line 40, in write
    self.__convertor.write(text)

  File "C:\Anaconda2\lib\site-packages\colorama\ansitowin32.py", line 141, in write
    self.write_and_convert(text)

  File "C:\Anaconda2\lib\site-packages\colorama\ansitowin32.py", line 169, in write_and_convert
    self.write_plain_text(text, cursor, len(text))

  File "C:\Anaconda2\lib\site-packages\colorama\ansitowin32.py", line 174, in write_plain_text
    self.wrapped.write(text[start:end])

  File "C:\Anaconda2\lib\site-packages\ipykernel\iostream.py", line 317, in write
    self._buffer.write(string)

ValueError: I/O operation on closed file

Activity

  1. lrq3000 commented on Jul 20, 2016

    @lrq3000
    Member

    That's a really weird error. I can't guess why there is an IO error since tqdm streams to the console normally. But it seems colorama might be at fault here.

    Could you please try to uninstall colorama and retry your script several times in order to see if the error happens again? (you will need to restart your ipython notebook kernel before to force the reloading of modules).

    If this still produces an IO error randomly, could you please give us the script (or better a minimal example)?

  2. jveitchmichaelis commented on Jul 21, 2016

    @jveitchmichaelis

    Here is code that fails for me. The function takes a list of images from two cameras and stacks them together to produce one output image.

    Like the OP, I use tqdm a lot and most of the time it works wonderfully. However anything involving a loop over files seems to cause sporadic problems.

    import numpy as np
    from tqdm import tqdm
    import cv2
    
    def stack_images(l, r):
        lim = cv2.imread(l[0], cv2.IMREAD_GRAYSCALE)
        rim = cv2.imread(r[0], cv2.IMREAD_GRAYSCALE)
    
        for l_i, r_i in tqdm(zip(l[1:],r[1:])):
                lim_new = cv2.imread(l_i, cv2.IMREAD_GRAYSCALE)
                if lim_new is None:
                    raise IOError
                lim = np.maximum(lim, lim_new)
                rim_new = cv2.imread(r_i, cv2.IMREAD_GRAYSCALE)
                if rim_new is None:
                    raise IOError
                rim = np.maximum(rim, rim_new)
        return lim, rim
    

    Called something like:

    import glob
    left_ims = glob.glob('./left_*.png')
    right_ims = glob.glob('./right_*.png')
    
    left_stack, right_stack = stack_images(left_ims, right_ims)
    
    

    I don't get an exception if I remove the tqdm wrapper.

    Hope that helps!

  3. samuelleach commented on Jul 22, 2016

    @samuelleach

    Just to note that I have observed the same occasional ValueError. It is hard to reproduce. Maybe the following is helpful: python 2.7. Error occurred after approx 400K iteration of a 15 minute job.

  4. casperdcl commented on Jul 22, 2016

    @casperdcl
    SponsorMember

    This is... frustrating. I don't want to say this, but... bug in python?

  5. casperdcl commented on Jul 23, 2016

    @casperdcl
    SponsorMember

    @jveitchmichaelis maybe you need an extra wait?

    import numpy as np
    from tqdm import tqdm
    import cv2
    import time.sleep
    
    def stack_images(r):
        rim = cv2.imread(r[0], cv2.IMREAD_GRAYSCALE)
        for r_i in tqdm(r[1:]):
                rim_new = cv2.imread(r_i, cv2.IMREAD_GRAYSCALE)
                if rim_new is None:
                    raise IOError
                rim = np.maximum(rim, rim_new)
        time.sleep(1)  # wait for possible io to fininsh?
        return rim
    
    # ...
    
    import glob
    left_ims = glob.glob('./left_*.png')
    right_ims = glob.glob('./right_*.png')
    
    # ensure same length
    left_ims = left_ims[:min(len(left_ims), len(right_ims))]
    right_ims = right_ims[:len(left_ims)]
    
    # stack
    left_stack = stack_images(left_ims)
    right_stack = stack_images(right_ims)
  6. jveitchmichaelis commented on Jul 23, 2016

    @jveitchmichaelis

    I'll try that, although I have around 30-40k images to process so it might take a while (I guess 1s was for demonstration rather than anything else). Typically this thing runs at around 40 iterations per second (i.e. 80 images/sec) and there isn't a problem when tqdm isn't used.

    There was a reason I read in both images in the same function, but I forget why (I think previously I was doing a joint operation on them as I read them in) :)

  7. casperdcl commented on Jul 23, 2016

    @casperdcl
    SponsorMember

    My reasoning about the 1 second wait was that maybe your entire program is ending just before all of the file handles are properly closed or something. It's just a wait right at the end of the program.

  8. jveitchmichaelis commented on Jul 23, 2016

    @jveitchmichaelis

    Ah sorry misread that! But it fails mid-sequence, not at the end. At least I think it does, I'll check.

  9. gcr commented on Jul 27, 2016

    @gcr

    I have this bug too and it's frustrating. It only began very recently (within the last week); tqdm was rock-solid before that.

    In a long-running task, sometimes tqdm throws an exception 30% of the way in. It doesn't just happen at the end.

    (I'm using tqdm inside an IPython Notebook. Will report back with a traceback if it's separate from above.)

  10. gcr commented on Jul 27, 2016

    @gcr

    My traceback appears in the same place, but it also happens on Linux:

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    
    /home/gcr/share/behance/mturk-tools/incremental_learning.pyc in load(mids)
         36         return np.array([
         37             h5_features[mid_to_idx[mid]]
    ---> 38             for mid in tqdm(list(mids))
         39         ])
         40     return load
    
    /usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.pyc in __iter__(self)
        620                              else ncols),
        621                             self.desc, ascii, unit, unit_scale,
    --> 622                             1 / avg_time if avg_time else None, bar_format))
        623 
        624                         if self.pos:
    
    /usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.pyc in print_status(s)
         96         def print_status(s):
         97             len_s = len(s)
    ---> 98             fp_write('\r' + s + (' ' * max(last_printed_len[0] - len_s, 0)))
         99             fp.flush()
        100             last_printed_len[0] = len_s
    
    /usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.pyc in fp_write(s)
         90 
         91         def fp_write(s):
    ---> 92             fp.write(_unicode(s))
         93 
         94         last_printed_len = [0]  # closure over mutable variable (fast)
    
    /usr/local/lib/python2.7/dist-packages/ipykernel/iostream.pyc in write(self, string)
        315 
        316             is_child = (not self._is_master_process())
    --> 317             self._buffer.write(string)
        318             if is_child:
        319                 # newlines imply flush in subprocesses
    
    ValueError: I/O operation on closed file
    
  11. casperdcl commented on Jul 27, 2016

    @casperdcl
    SponsorMember

    When you say only in the last week, do you mean you regularly update your
    python packages? What was the last tqdm version which was stable for you?
    When you get the error, is it always at 30% through that loop, or sometimes
    earlier/later?

    An easy fix for now would be for me to catch the exception internally and
    ignore it, but I'd still like to get to the real cause.

    On 27 Jul 2016 5:05 p.m., "gcr" notifications@github.com wrote:

    My traceback appears in the same place, but it also happens on Linux:


    ValueError Traceback (most recent call last)

    /home/gcr/share/behance/mturk-tools/incremental_learning.pyc in load(mids)
    36 return np.array([
    37 h5_features[mid_to_idx[mid]]
    ---> 38 for mid in tqdm(list(mids))
    39 ])
    40 return load

    /usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.pyc in iter(self)
    620 else ncols),
    621 self.desc, ascii, unit, unit_scale,
    --> 622 1 / avg_time if avg_time else None, bar_format))
    623
    624 if self.pos:

    /usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.pyc in print_status(s)
    96 def print_status(s):
    97 len_s = len(s)
    ---> 98 fp_write('\r' + s + (' ' * max(last_printed_len[0] - len_s, 0)))
    99 fp.flush()
    100 last_printed_len[0] = len_s

    /usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.pyc in fp_write(s)
    90
    91 def fp_write(s):
    ---> 92 fp.write(_unicode(s))
    93
    94 last_printed_len = [0] # closure over mutable variable (fast)

    /usr/local/lib/python2.7/dist-packages/ipykernel/iostream.pyc in write(self, string)
    315
    316 is_child = (not self._is_master_process())
    --> 317 self._buffer.write(string)
    318 if is_child:
    319 # newlines imply flush in subprocesses

    ValueError: I/O operation on closed file

    —
    You are receiving this because you commented.
    Reply to this email directly, view it on GitHub
    #202 (comment), or mute
    the thread
    https://github.com/notifications/unsubscribe-auth/AKR9mwe_zKf8RlPCLg9evL1h7DjfHRpEks5qZ4GjgaJpZM4JROPU
    .

  12. gcr commented on Jul 27, 2016

    @gcr

    Pardon. I rarely update python packages, so I don't think they changed from then until now.

    However, one confusing factor could be that this week I am doing longer-running tasks. The total output produced by tqdm is much larger than it was before. That's one difference. Maybe it could be overflowing some internal buffer? (Is there a way to measure the amount of raw output that tqdm produces?)

    tqdm doesn't always throw in the same place though. Sometimes the progress bar completes; sometimes it stops at 50%. It usually works.

  13. casperdcl commented on Jul 27, 2016

    @casperdcl
    SponsorMember

    thanks.

    can anyone confirm this is still a bug using tqdm>=4.7.5?

    On 27 Jul 2016 5:49 p.m., "gcr" notifications@github.com wrote:

    Pardon. I rarely update python packages, so I don't think they changed
    from then until now.

    However, one confusing factor could be that this week I am doing
    longer-running tasks. The total output produced by tqdm is much larger than
    it was before. That's one difference. Maybe it could be overflowing some
    internal buffer? (Is there a way to measure the amount of raw output that
    tqdm produces?)

    tqdm doesn't always throw in the same place though. Sometimes the progress
    bar completes; sometimes it stops at 50%. It usually works.

    —
    You are receiving this because you commented.
    Reply to this email directly, view it on GitHub
    #202 (comment), or mute
    the thread
    https://github.com/notifications/unsubscribe-auth/AKR9mzYVDN9UipBZ4iyW1YA5hODR1WTGks5qZ4wegaJpZM4JROPU
    .

  14. gcr commented on Jul 30, 2016

    @gcr

    aha! i'm still seeing this behavior occasionally on 4.8.1-b71261d. i didn't save a traceback (foolish me!), will post one if i see it again.

  15. 12 remaining items

  16. samuelleach commented on Aug 16, 2016

    @samuelleach

    I have also been getting this issue on Jupyter notebook.

    @lrq3000 Would it be possible if you can post a link to the corresponding bug report you mentioned in iPython notebook repo? I'd be glad to know when this issue will be resolved 👍

  17. lrq3000 commented on Aug 16, 2016

    @lrq3000
    Member

    @samuelleach The bug report is here: ipython/ipython#9168 and the PR that will supposedly fix the issue by v4.4 here: ipython/ipykernel#123

  18. lrq3000 commented on Aug 16, 2016

    @lrq3000
    Member

    Ah the ipykernel v4.4.1 was released on 2016-08-09: https://pypi.python.org/pypi/ipykernel

    So we can soon try again this issue (most packages like Anaconda gets updated one month after the pypi release usually).

  19. samuelleach commented on Aug 16, 2016

    @samuelleach

    Thanks for the info. Anecdotally, based on the last few hours usage of tqdm, things are stable since I did conda update ipykernel and got version 4.4.1 👍

  20. casperdcl commented on Oct 19, 2016

    @casperdcl
    SponsorMember

    Not a bug with tqdm, bug fixed elsewhere

  21. lrq3000 commented on Oct 25, 2016

    @lrq3000
    Member

    At least we hope it's fixed, I did not update ipywidgets so I did not test
    it myself. Anyone, please feel free to reopen this issue if the issue
    arises again with the latest ipywidgets!

    2016-10-19 17:25 GMT+02:00 Casper da Costa-Luis notifications@github.com:

    Not a bug with tqdm, bug fixed elsewhere

    —
    You are receiving this because you were mentioned.
    Reply to this email directly, view it on GitHub
    #202 (comment), or mute
    the thread
    https://github.com/notifications/unsubscribe-auth/ABES3pfjoeT9YhitICxGZIUSnALU34dwks5q1jaGgaJpZM4JROPU
    .

  22. casperdcl commented on Nov 13, 2016

    @casperdcl
    SponsorMember

    still, it's nice to know that tqdm appears to be more stable that ipython, hah

  23. Varun-Epi commented on Aug 16, 2018

    @Varun-Epi

    @lrq3000 @casperdcl Hey, I am getting this error and am not using jupyter/ipython. Its quite random, in my earlier experiments tqdm was working perfectly fine, but now this error is recurring in some of the experiments.

      File "train-ner.py", line 367, in train
        with tqdm.tqdm(total=n_train_words, leave=False) as pbar:
      File "/usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.py", line 812, in __init__
        self.sp(self.__repr__(elapsed=0))
      File "/usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.py", line 176, in print_status
        fp_write('\r' + s + (' ' * max(last_len[0] - len_s, 0)))
      File "/usr/local/lib/python2.7/dist-packages/tqdm/_tqdm.py", line 169, in fp_write
        fp.write(_unicode(s))
    IOError: [Errno 32] Broken pipe
    

    Details

    Platform : Ubuntu 16.04
    tqdm : 4.19.9
    
  24. ABCDeath commented on May 15, 2020

    @ABCDeath

    That's realy weird, but I can reproduce this error everytime I debug a Python file like this with PyCharm:

    from tqdm import tqdm
    
    if __name__ == '__main__':
        for _ in tqdm(range(10)):
            pass

    When I run the same file in PyCharm it doesn't throw this exception.
    MacOs 10.15.4, Python 3.8.2, tqdm 4.46.0.

  25. changed the title [-]I/O operation on closed file bug[/-] [+]PyCharm debugging mode: I/O operation on closed file[/+] on May 16, 2020
  26. changed the title [-]PyCharm debugging mode: I/O operation on closed file[/-] [+]IPython & PyCharm debugging mode: I/O operation on closed file[/+] on May 16, 2020
  27. casperdcl commented on May 16, 2020

    @casperdcl
    SponsorMember

    pretty sure this is an upstream bug with PyCharm, would be helpful if someone finds/posts an issue. See e.g. #203 for other PyCharm issues which are solved upstream.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions