can't flush print when download open-uri

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default can't flush print when download open-uri

    I'm confused. why won't this print? I tried flushing the $defout and $stdout as well as the source_file_io, but that doesn't work either.

    # download
    prioritized_locations.each do |location|
    begin
    source_file_io = File.open(self.source_path,'w')
    remote_file = open(location)
    while incoming = remote_file.read(512)
    source_file_io.write(incoming)
    # THIS WON'T THIS PRINT!!!!!!!!!!!
    print "\ca{source_file_io.pos}KB/#{@source_size}KB"
    end
    rescue
    puts "#{location} failed"
    remote_file.close unless remote_file.nil?
    source_file_io.close unless source_file_io.nil?
    next # try next location
    else
    puts ' Done.'
    break # we got it
    ensure
    remote_file.close unless remote_file.nil?
    source_file_io.close unless source_file_io.nil?
    end
    end

    -t0

    T. Onoma Guest

  2. Similar Questions and Discussions

    1. Why does word copy appear when I print a pdfmanual (download from Canon)
      Every page has the word copy printed on top of the words. Can this be turned off?
    2. way to download AND open a PDF from within a PDF 'contents page'?
      I'm not even sure if this is possible.... i'm just assuming. I have core 'contents pages' for a large technical document (distributed via CD). I...
    3. Can't print anything after download
      have windows xp sp1 home. Just downloaded free 30 trial of adobe 6.0 professional. Now my printere will not work. I have an epson which is connected...
    4. download via header using flush()?
      hi, i'm looking for a possibility to start a download. here is my current code: header("Content-Type: $_contenttype");...
    5. Download vs. Open
      Is it the same client? "Larry Gillstrom" <lgillstrom@dccnet.com> wrote in message news:ugZmIfSSDHA.1912@TK2MSFTNGP12.phx.gbl...
  3. #2

    Default Re: can't flush print when download open-uri


    Maybe it's because you didn't terminate with "\n".

    "T. Onoma" <transami@runbox.com> schrieb im Newsbeitrag
    news:E1AJs2a-0004hd-RA@odie.runbox.com...
    > I'm confused. why won't this print? I tried flushing the $defout and
    $stdout as well as the source_file_io, but that doesn't work either.

    I'd try $defout.sync=true then you don't need the flushes.
    > # download
    > prioritized_locations.each do |location|
    > begin
    > source_file_io = File.open(self.source_path,'w')
    > remote_file = open(location)
    > while incoming = remote_file.read(512)
    > source_file_io.write(incoming)
    > # THIS WON'T THIS PRINT!!!!!!!!!!!
    > print "\ca{source_file_io.pos}KB/#{@source_size}KB"
    did you mean

    print "#{source_file_io.pos}KB/#{@source_size}KB\n"

    ?
    > end
    > rescue
    > puts "#{location} failed"
    > remote_file.close unless remote_file.nil?
    > source_file_io.close unless source_file_io.nil?
    Closing here is superfluous since "ensure" takes care of that.
    > next # try next location
    superfluous as well.
    > else
    > puts ' Done.'
    > break # we got it
    superfluous as well.
    > ensure
    > remote_file.close unless remote_file.nil?
    > source_file_io.close unless source_file_io.nil?
    > end
    > end
    In the end we get much cleaner code:

    require 'open-uri'

    # download
    $defout.sync = true

    prioritized_locations.each do |location|
    begin
    File.open(self.source_path,'w') do |source_file_io|
    open(location) do |remote_file|
    while incoming = remote_file.read(512)
    source_file_io.write(incoming)
    print "#{source_file_io.pos}KB/#{@source_size}KB "
    end
    end
    end
    rescue => e
    $stderr.puts "#{location} failed: #{e}"
    else
    puts 'Done.'
    end
    end

    This works for me - and does print.

    robert

    Robert Klemme Guest

  4. #3

    Default Re: can't flush print when download open-uri

    no one responded to this and i still can't get it to work. please help. let me rephrase very simply:

    downloading file with open_uri and can't flush print to report download progress.

    thanks,
    -t0

    > I'm confused. why won't this print? I tried flushing the $defout and $stdout as well as the source_file_io, but that doesn't work either.
    >
    > # download
    > prioritized_locations.each do |location|
    > begin
    > source_file_io = File.open(self.source_path,'w')
    > remote_file = open(location)
    > while incoming = remote_file.read(512)
    > source_file_io.write(incoming)
    > # THIS WON'T THIS PRINT!!!!!!!!!!!
    > print "\ca{source_file_io.pos}KB/#{@source_size}KB"
    > end
    > rescue
    > puts "#{location} failed"
    > remote_file.close unless remote_file.nil?
    > source_file_io.close unless source_file_io.nil?
    > next # try next location
    > else
    > puts ' Done.'
    > break # we got it
    > ensure
    > remote_file.close unless remote_file.nil?
    > source_file_io.close unless source_file_io.nil?
    > end
    > end
    >
    > -t0
    >
    >
    T. Onoma Guest

  5. #4

    Default Re: can't flush print when download open-uri

    Hi,

    From: "T. Onoma" <transami@runbox.com>
    >
    > no one responded to this and i still can't get it to work. please help.
    > let me rephrase very simply:
    >
    > downloading file with open_uri and can't flush print to report download progress.
    >
    [...]
    >
    > > while incoming = remote_file.read(512)
    > > source_file_io.write(incoming)
    > > # THIS WON'T THIS PRINT!!!!!!!!!!!
    > > print "\ca{source_file_io.pos}KB/#{@source_size}KB"
    > > end
    Have you tried stdout.sync = true ?

    --------------------------------------------------------------- IO#sync=
    ios.sync = aBoolean -> aBoolean
    ------------------------------------------------------------------------
    Sets the ``sync mode'' to true or false. When sync mode is true,
    all output is immediately flushed to the underlying operating
    system and is not buffered internally. Returns the new state. See
    also IO#fsync.
    f = File.new("testfile")
    f.sync = true
    (produces no output)


    Hope this helps,

    Bill



    Bill Kelly Guest

  6. #5

    Default Re: can't flush print when download open-uri

    no one responded to this and i still can't get it to work. please help. let me rephrase very simply:

    downloading file with open_uri and can't flush print to report download progress.

    thanks,
    -t0

    > I'm confused. why won't this print? I tried flushing the $defout and $stdout as well as the source_file_io, but that doesn't work either.
    >
    > # download
    > prioritized_locations.each do |location|
    > begin
    > source_file_io = File.open(self.source_path,'w')
    > remote_file = open(location)
    > while incoming = remote_file.read(512)
    > source_file_io.write(incoming)
    > # THIS WON'T THIS PRINT!!!!!!!!!!!
    > print "\ca{source_file_io.pos}KB/#{@source_size}KB"
    > end
    > rescue
    > puts "#{location} failed"
    > remote_file.close unless remote_file.nil?
    > source_file_io.close unless source_file_io.nil?
    > next # try next location
    > else
    > puts ' Done.'
    > break # we got it
    > ensure
    > remote_file.close unless remote_file.nil?
    > source_file_io.close unless source_file_io.nil?
    > end
    > end
    >
    > -t0
    T. Onoma Guest

  7. #6

    Default Re: can't flush print when download open-uri

    > Have you tried stdout.sync = true ?

    Yep. Dosen't work either. Thanks though. Boy is this frustrating. There must be a simple way to do this.


    P.S. SORRY ABOUT THE MULTIPLE POST. MAIL SERVER ACTING UP.

    T. Onoma Guest

  8. #7

    Default Re: can't flush print when download open-uri

    From: "T. Onoma" <transami@runbox.com>
    >
    > > Have you tried stdout.sync = true ?
    >
    > Yep. Dosen't work either. Thanks though. Boy is this frustrating.
    > There must be a simple way to do this.
    Hmm... Well I only have a bunch of questions then :)

    What platform are you running on? What ruby version? Is this
    a standalone script, or is it called from some other process (such
    as a CGI script called by Apache)?

    Do you eventually see any of your printouts at all? That is, if
    a linefeed is eventually printed, do all the "buffered" prints
    eventually show up?

    Also, in:
    > # THIS WON'T THIS PRINT!!!!!!!!!!!
    > print "\ca{source_file_io.pos}KB/#{@source_size}KB"
    Do you really want a control-A? And is the sharp sign really
    ommitted there before the open brace at \ca{ ?


    Regards,

    Bill



    Bill Kelly Guest

  9. #8

    Default Re: can't flush print when download open-uri

    Received: Fri, 14 Nov 2003 10:30:45 +0900
    And lo T. wrote:
    > > (snip)
    > > source_file_io.write(incoming)
    > > # THIS WON'T THIS PRINT!!!!!!!!!!!
    > > print "\ca{source_file_io.pos}KB/#{@source_size}KB"
    STDOUT.flush
    > > end
    > > rescue
    > > (/snip)
    Gregory Millam Guest

  10. #9

    Default Re: can't flush print when download open-uri

    On Friday, November 14, 2003, 12:05:16 PM, T. wrote:
    > no one responded to this and i still can't get it to work. please help. let me rephrase very simply:
    > downloading file with open_uri and can't flush print to report download progress.
    >> [snip]
    >> # THIS WON'T THIS PRINT!!!!!!!!!!!
    >> print "\ca{source_file_io.pos}KB/#{@source_size}KB"


    I doubt it has anything to do with open-uri. Try STDOUT.flush after
    the print.

    BTW, what does print "\ca{source_file_io.pos}" achieve? Specifically,
    the "\ca".

    Gavin


    Gavin Sinclair Guest

  11. #10

    Default Re: can't flush print when download open-uri

    > From: "T. Onoma" <transami@runbox.com>
    > What platform are you running on? What ruby version? Is this
    > a standalone script, or is it called from some other process (such
    > as a CGI script called by Apache)?
    linux, 1.8, standalone
    > Do you eventually see any of your printouts at all? That is, if
    > a linefeed is eventually printed, do all the "buffered" prints
    > eventually show up?
    when the while loop finishes it all comes flying out. have tried flush on stdout and file's io.
    > Also, in:
    > > # THIS WON'T THIS PRINT!!!!!!!!!!!
    > > print "\ca{source_file_io.pos}KB/#{@source_size}KB"
    >
    > Do you really want a control-A? And is the sharp sign really
    > ommitted there before the open brace at \ca{ ?
    typo, ctrl-a i had hoped would move to beginning of line, but can't test obviously. i have since replaced this line with a call to Ruby/ProgressBar from RAA. but it still the same problem.

    thanks,
    -t0

    T. Onoma Guest

  12. #11

    Default Re: can't flush print when download open-uri

    Hi,

    At Fri, 14 Nov 2003 11:01:56 +0900,
    Gavin Sinclair wrote:
    > BTW, what does print "\ca{source_file_io.pos}" achieve? Specifically,
    > the "\ca".
    "\c" means Control key modifier, that is "\ca" is equivalent to
    "\001".

    $ ruby -e 'print "\ca"'|od -tx1
    0000000 01
    0000001

    --
    Nobu Nakada

    nobu.nokada@softhome.net Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139