質問

I can't figure why in my code I get a weird IOError [Errno 4] Interrupted system call.

The example below is a dummy one, but it allows (at least on my Linux laptop) to reproduce the error:

import subprocess as sp

def dummyfun():
    p1 = sp.Popen(['ls','-lah'], stderr=sp.STDOUT, stdout=sp.PIPE, close_fds=True)
    p2 = sp.Popen(['grep','.'],stdin=p1.stdout, stdout=sp.PIPE, close_fds=True)
    p1.stdout.close()
    return p2.stdout

def dummyfun2(fo):
    for l in fo:
        print l,

def dummyfun3():
    fo = dummyfun()
    dummyfun2(fo)
    fo.close()

Calling dummyfun3 basically just prints on the screen a list of all the files whith a dot in their name (in my case I am in a folder with about 100 files, and all of them have some extension and therefore a dot).

If I call the function as dummyfun3() it just works.

However if I call it in a loop like:

for i in range(1000):
    dummyfun3()

after a few iterations the code stops (usually i is between 5 and 15) displaying the following error message:

<ipython-input-213-a47ea086386d> in <module>()
      1 for i in range(1000):
----> 2     dummyfun3()
      3 

<ipython-input-205-21366f183162> in dummyfun3()
      1 def dummyfun3():
      2     fo = dummyfun()
----> 3     dummyfun2(fo)
      4     fo.close()
      5 

<ipython-input-204-1c08d906020b> in dummyfun2(fo)
      1 def dummyfun2(fo):
      2     for l in fo:
----> 3         print l,
      4 

IOError: [Errno 4] Interrupted system call

Why such an error and how can I prevent it?

役に立ちましたか?

解決

The error occurs on the print l, line: that looks more like an ipython bug. As you said in the comments already, it works on the command line. You should try the latest development version of ipython and if it still fails, report it as a bug, I'd say.

他のヒント

IPython developer here. For what it's worth, it's unlikely that this is an IPython bug - at least I'm unable to reproduce it in either master, nor on 0.13.2 (64-bit Debian/sid). Luca filed this as issue #3884, so we'll see where it goes from there.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top