Errno :: EINVAL:无效的参数@ io_write

时间:2018-01-26 16:04:54

标签: ruby

我有一段Ruby代码,它从S3获取一个zip文件并将其保存在本地。

$(function () {
    $("#oe-slide-left").on('click', function () {
        SlideLeft();
    });

    $("#oe-slide-right").on('click', function () {
        SlideRight();
    });
});

function SlideLeft() {
    var countLeft = $('#oe-testemonials').find('.oe-testemonial:visible').each(function () { }).length;
    var countRight = $('#oe-testemonials').find('.oe-testemonial:hidden').each(function () { }).length;

    if (countLeft > 3) {
        $("#oe-testemonials div.oe-testemonial:visible").first().animate({ width: 'toggle' }, 350);
    }

    if (countLeft === 4) {
        $("#oe-slide-left").hide();
    }

    if (countRight === 0) {
        $("#oe-slide-right").show();
    }
}

function SlideRight() {
    var countLeft = $('#oe-testemonials').find('.oe-testemonial:visible').each(function () { }).length;
    var countRight = $('#oe-testemonials').find('.oe-testemonial:hidden').each(function () { }).length;

    if (countRight > 0) {
        $("#oe-testemonials div.oe-testemonial:hidden").last().animate({ width: 'toggle' }, 350);
    }

    if (countRight <= 1) {
        $("#oe-slide-right").hide();
    }

    if (countLeft === 3)
    {
        $("#oe-slide-left").show();
    }
}

.oe-testemonials {
        margin-top: 40px;
        width: 100%;
        overflow: hidden;
        white-space: nowrap;
        position: relative;

        .slider-left-arrow, .slider-right-arrow {
            position: absolute;
            z-index: 1000;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;

            &:hover {
                background: #efe5e5;
                opacity: 0.8;
            }
        }

        .slider-left-arrow {
            left: 15px;
        }

        .slider-right-arrow {
            right: 15px;
        }

        .oe-testemonial {
            width: 33%;
            display: inline-block;
            margin: auto;
            white-space: normal;

            .image-wrapper {
                width: 100px;
                height: 100px;
                position: relative;
                overflow: hidden;
                border-radius: 50%;
                margin: auto;

                img {
                    display: inline;
                    margin: 0 auto;
                    height: 100%;
                    width: auto;
                }
            }

            .description, .name, .title, .location {
                width: 90%;
                max-width: 300px;
                text-align: center;
                margin: auto;
            }

            .description {
                border-bottom: 1px solid #999;
                padding-bottom: 5px;
                margin-bottom: 5px;
                color: #413880;
            }

            .name {
                font-family: 'Just Another Hand', cursive;
                font-size: 24px;
            }

            .location {
                color: #B3B3B3;
            }
        }
}

.description{
  width: 100px !important;
}

<div id="oe-testemonials" class="oe-testemonials">
        <img id="oe-slide-left" src="/Images/left_arrow.png" alt="Click To Slide Left" class="slider-left-arrow">
        <img id="oe-slide-right" src="/Images/right_arrow.png" alt="Click To Slide Right" class="slider-right-arrow" style="display: none;">
    <div class="oe-testemonial" style="display: inline-block;"><div class="image-wrapper"><img src="/Images/people/Erin_Hartigan-6947_sm.jpg" alt="Portfolio Image"></div><div class="description"><span>I think that OrthoEvidence is wonderful. It’s great that there are new avenues for people to learn about the evidence and have it summarized, which allows for a more transparent transfer of research to both practitioners and patients.</span></div><div class="name"><span>Dr. Erin Hartigan</span></div><div class="title"><span>Assistant Professor of Physical Therapy</span></div><div class="location"><span>University of New England</span></div></div><div class="oe-testemonial"><div class="image-wrapper"><img src="/Images/people/HiteshGopalan.jpg" alt="Portfolio Image"></div><div class="description"><span>As an Editor of a book and an associated website, I have been following 20- 30 journals every month. But now I have shifted my primary focus to OrthoEvidence where I get all quality information in one place.</span></div><div class="name"><span>Dr. Hitesh Gopalan</span></div><div class="title"><span>Editor</span></div><div class="location"><span>orthopaedicprinciples.com</span></div></div><div class="oe-testemonial"><div class="image-wrapper"><img src="/Images/people/DavidGryfe_sm.jpg" alt="Portfolio Image"></div><div class="description"><span>OrthoEvidence raises the bar on timely and relevant reviews of the medical literature. This is an indispensible service for every practitioner who strives to stay current.</span></div><div class="name"><span>Dr. David Gryfe</span></div><div class="title"><span>Chiropractor</span></div><div class="location"><span>Chiropractic Sports Specialist</span></div></div><div class="oe-testemonial"><div class="image-wrapper"><img src="/Images/people/AaronKrych_sm.jpg" alt="Portfolio Image"></div><div class="description"><span>OrthoEvidence allows the busy practicing surgeon, in a short period of time, to stay up to date with what is important with the changes in the literature.</span></div><div class="name"><span>Dr. Aaron Krych</span></div><div class="title"><span>Orthopaedic Surgeon</span></div><div class="location"><span>Mayo Clinic</span></div></div><div class="oe-testemonial"><div class="image-wrapper"><img src="/Images/people/JonathanRonquillo_sm.jpg" alt="Portfolio Image"></div><div class="description"><span>OrthoEvidence has made me more critical of available papers. It has made me look back at certain existing protocols...to improve existing practice.</span></div><div class="name"><span>Dr. Jonathan Ronquillo</span></div><div class="title"><span>Orthopaedic Surgeon</span></div><div class="location"><span>Asian Hospital and Medical Center</span></div></div><div class="oe-testemonial"><div class="image-wrapper"><img src="/Images/people/FrankSmith_sm.jpg" alt="Portfolio Image"></div><div class="description"><span>OrthoEvidence is the flag ship for enabling us to keep current whilst still working 60 hour weeks.</span></div><div class="name"><span>Dr. Frank Smith</span></div><div class="title"><span>Orthopaedic Surgeon</span></div><div class="location"><span>McMaster University</span></div></div><div class="oe-testemonial"><div class="image-wrapper"><img src="/Images/people/MarcosBritto_sm.jpg" alt="Portfolio Image"></div><div class="description"><span>OE is medical information of unparalleled quality, with papers revised, and a newsletter available in my inbox. It is the fastest and easiest way to stay updated on Orthopedics.</span></div><div class="name"><span>Dr. Marcos Britto da Silva</span></div><div class="title"><span>Orthopaedic Surgeon</span></div><div class="location"><span>Federal University of Rio de Janeiro</span></div></div></div>

此代码适用于某些文件,但不适用于其他文件。

对于那些不起作用的我,我得到了这个错误:

  

Errno :: EINVAL:无效的参数@ io_write - /path/to/file.zip

这可能是什么原因?

2 个答案:

答案 0 :(得分:2)

身体可能太大了。错误信息确实没有多大意义,但这可能是原因。

要缓解此问题,请将其写入较小的块中。最简单(尽管不是最快)的方式是逐字逐句:

File.open('file_name.txt', 'wb') do |file|
  body.each_char { |char| file.write char }
end

答案 1 :(得分:0)

使用StringIO.read( chunk_size )

@ndnenkov是绝对正确的。文件本身可能太大而无法存储在内存中。此限制因操作系统,配置和可用资源而异,因系统而异。

但是,对于大文件,使用each_char可能会非常慢。我对一些方法和块大小以及一些不同的文件大小进行了基准测试,以进行比较。

13027836 bytes
13.03 megabytes
                           user     system      total        real
write:                 0.000767   0.004443   0.005210 (  0.005312)
each_char:             5.756789   0.032231   5.789020 (  5.797378)
each_byte:             8.997680   0.067377   9.065057 (  9.179755)
StringIO 1 KB:         0.004029   0.006966   0.010995 (  0.011648)
StringIO 1 MB:         0.016100   0.007118   0.023218 (  0.023509)
StringIO 10 MB:        0.003347   0.006924   0.010271 (  0.010334)
StringIO 100 MB:       0.000456   0.003758   0.004214 (  0.007080)
StringIO 1 GB:         0.000468   0.003787   0.004255 (  0.005037)


706583272 bytes
0.71 gigabytes
                           user     system      total        real
write:                 0.001035   0.285726   0.286761 (  0.324529)
each_char:           362.444086   1.820033 364.264119 (365.362415)
each_byte:           548.788409   3.254867 552.043276 (553.390843)
StringIO 1 KB:         0.310588   0.331768   0.642356 (  0.697581)
StringIO 1 MB:         0.302101   0.325285   0.627386 (  0.671933)
StringIO 10 MB:        0.254845   0.294017   0.548862 (  0.895430)
StringIO 100 MB:       0.471879   0.429933   0.901812 (  1.181456)
StringIO 1 GB:         0.000471   0.260011   0.260482 (  0.653977)


5577825775 bytes
5.58 gigabytes
                            user     system       total         real
write:                     error      error       error        error
each_char:           2926.215017  38.658114 2964.873131 (3008.319599)
each_byte:           4305.082576  35.090730 4340.173306 (4363.944091)
StringIO 1 KB:          4.145908   3.962275    8.108183 (   9.490059)
StringIO 1 MB:          3.741062   2.779802    6.520864 (   7.423770)
StringIO 10 MB:         2.916272   2.553926    5.470198 (   6.271349)
StringIO 100 MB:        4.262794   3.007702    7.270496 (  10.986725)
StringIO 1 GB:          2.063459   4.572225    6.635684 (   9.212933)

基于此,我们最终使用了块大小为1MB的StringIO,但是您将需要尝试以下特定情况:

File.open('file_name.txt', 'wb') do |file|
  bodyIO = StringIO.new( body )
  until bodyIO.eof?
    file.write( bodyIO.read( 1024*1024 ) ) # Write 1 MB chunks at a time to avoid Errno::EINVAL errors like `Invalid argument @ io_fread` and `Invalid argument @ io_write`.
  end
end