[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Interactive Processing of print

dmatrix00d

6/30/2006 1:59:00 PM

For example, I have a python program that does

print "hello"
time.sleep(10)
print "world"

and i want to print this in ruby

"hello foo"
(wait 10 seconds)
"world foo"

I tried :

IO.popen("python example.py") do |p|
p.each do |line|
puts line + "foo"
end
end

and it does everything except , it waits the 10 seconds before it
begins processing so it is not interactive

14 Answers

ts

6/30/2006 2:15:00 PM

0

>>>>> "d" == dmatrix00d <dmatrix00d@gmail.com> writes:

d> and it does everything except , it waits the 10 seconds before it
d> begins processing so it is not interactive

Well the problem is in the P language, fatally :-)

Seriously, with these scripts

moulon% cat b.rb
#!/usr/bin/ruby
IO.popen("ruby c.rb") do |p|
p.each do |line|
puts line + "foo"
end
end
moulon%

moulon% cat c.rb
#!/usr/bin/ruby
puts "hello"
sleep(10)
puts "word"
moulon%

You'll have the same result, but if you change the script c.rb to add
'stdout.sync = true' it will work as expected

moulon% cat c.rb
#!/usr/bin/ruby
$stdout.sync = true
puts "hello"
sleep(10)
puts "word"
moulon%

this mean that, in your case, the problem is with the python script not
with ruby.


--

Guy Decoux

dmatrix00d

6/30/2006 2:19:00 PM

0

The example was just an example. I mainly want to be able to run any
program (not just a python program), and still intercept the print
statements and do something with them.

Thanks,

Sorry for the confusion

ts wrote:
> >>>>> "d" == dmatrix00d <dmatrix00d@gmail.com> writes:
>
> d> and it does everything except , it waits the 10 seconds before it
> d> begins processing so it is not interactive
>
> Well the problem is in the P language, fatally :-)
>
> Seriously, with these scripts
>
> moulon% cat b.rb
> #!/usr/bin/ruby
> IO.popen("ruby c.rb") do |p|
> p.each do |line|
> puts line + "foo"
> end
> end
> moulon%
>
> moulon% cat c.rb
> #!/usr/bin/ruby
> puts "hello"
> sleep(10)
> puts "word"
> moulon%
>
> You'll have the same result, but if you change the script c.rb to add
> 'stdout.sync = true' it will work as expected
>
> moulon% cat c.rb
> #!/usr/bin/ruby
> $stdout.sync = true
> puts "hello"
> sleep(10)
> puts "word"
> moulon%
>
> this mean that, in your case, the problem is with the python script not
> with ruby.
>
>
> --
>
> Guy Decoux

dmatrix00d

6/30/2006 2:19:00 PM

0

The example was just an example. I mainly want to be able to run any
program (not just a python program), and still intercept the print
statements and do something with them.

Thanks,

Sorry for the confusion

ts wrote:
> >>>>> "d" == dmatrix00d <dmatrix00d@gmail.com> writes:
>
> d> and it does everything except , it waits the 10 seconds before it
> d> begins processing so it is not interactive
>
> Well the problem is in the P language, fatally :-)
>
> Seriously, with these scripts
>
> moulon% cat b.rb
> #!/usr/bin/ruby
> IO.popen("ruby c.rb") do |p|
> p.each do |line|
> puts line + "foo"
> end
> end
> moulon%
>
> moulon% cat c.rb
> #!/usr/bin/ruby
> puts "hello"
> sleep(10)
> puts "word"
> moulon%
>
> You'll have the same result, but if you change the script c.rb to add
> 'stdout.sync = true' it will work as expected
>
> moulon% cat c.rb
> #!/usr/bin/ruby
> $stdout.sync = true
> puts "hello"
> sleep(10)
> puts "word"
> moulon%
>
> this mean that, in your case, the problem is with the python script not
> with ruby.
>
>
> --
>
> Guy Decoux

ts

6/30/2006 2:27:00 PM

0

>>>>> "E" == Erich Lin <dmatrix00d@gmail.com> writes:

E> The example was just an example. I mainly want to be able to run any
E> program (not just a python program), and still intercept the print
E> statements and do something with them.

You can't intercept the print statements if the program don't flush the
data. This is why I've added the line

$stdout.sync = true

to make work, like you expect, the script, because with this line, ruby
will flush the data after a \n

i.e when you use a print statement, the data is just put in a buffer and
this is only when the buffer is flushed that it's made available to
others programs.


--

Guy Decoux

dmatrix00d

6/30/2006 2:33:00 PM

0

this might be a stupid reply, but how come

system ("python example.py) works?

it prints

hello
(wait 10 seconds)
world.

i just want the effects of that, but do something with that print
statement

thanks

Erich Lin wrote:
> The example was just an example. I mainly want to be able to run any
> program (not just a python program), and still intercept the print
> statements and do something with them.
>
> Thanks,
>
> Sorry for the confusion
>
> ts wrote:
> > >>>>> "d" == dmatrix00d <dmatrix00d@gmail.com> writes:
> >
> > d> and it does everything except , it waits the 10 seconds before it
> > d> begins processing so it is not interactive
> >
> > Well the problem is in the P language, fatally :-)
> >
> > Seriously, with these scripts
> >
> > moulon% cat b.rb
> > #!/usr/bin/ruby
> > IO.popen("ruby c.rb") do |p|
> > p.each do |line|
> > puts line + "foo"
> > end
> > end
> > moulon%
> >
> > moulon% cat c.rb
> > #!/usr/bin/ruby
> > puts "hello"
> > sleep(10)
> > puts "word"
> > moulon%
> >
> > You'll have the same result, but if you change the script c.rb to add
> > 'stdout.sync = true' it will work as expected
> >
> > moulon% cat c.rb
> > #!/usr/bin/ruby
> > $stdout.sync = true
> > puts "hello"
> > sleep(10)
> > puts "word"
> > moulon%
> >
> > this mean that, in your case, the problem is with the python script not
> > with ruby.
> >
> >
> > --
> >
> > Guy Decoux

ts

6/30/2006 2:49:00 PM

0

>>>>> "E" == Erich Lin <dmatrix00d@gmail.com> writes:

E> this might be a stupid reply, but how come
E> system ("python example.py) works?

Try this

python example.py

it must do what you expect (i.e it print 'hello' wait 10 seconds). Then
try

python example.py | cat

and see what it do.



--

Guy Decoux

dmatrix00d

6/30/2006 3:34:00 PM

0

just doing python example.py ? how?
with backqutoes?

and cat assumes that this program will be run on *nix, which it may not
be.

and even if python example.py | cat using what?
ts wrote:
> >>>>> "E" == Erich Lin <dmatrix00d@gmail.com> writes:
>
> E> this might be a stupid reply, but how come
> E> system ("python example.py) works?
>
> Try this
>
> python example.py
>
> it must do what you expect (i.e it print 'hello' wait 10 seconds). Then
> try
>
> python example.py | cat
>
> and see what it do.
>
>
>
> --
>
> Guy Decoux

ts

6/30/2006 3:42:00 PM

0

>>>>> "E" == Erich Lin <dmatrix00d@gmail.com> writes:

E> just doing python example.py ? how?
E> with backqutoes?

no, no. just running interactively

python example.py

E> and cat assumes that this program will be run on *nix, which it may not
E> be.

yes, un*x system. Probably the command is `type' on a dos system, I don't know

E> and even if python example.py | cat using what?

on an un*x system if you execute

python example.py | cat

you'll wait 10 secondes before seeing the output 'hello' 'world' like for
your ruby program.

This is because I've created a pipe (`|') that you have this
result. When you use #popen this is like if you create a pipe.


--

Guy Decoux

dmatrix00d

6/30/2006 3:55:00 PM

0

I understand what you mean.
is there a way around that is my question, because system lets you get
around that, but you can't modify it, which i guess is what your first
example is saying.

i remember a post where someone said you can do this with popen, but it
won't work for all cases. i guess this is one of them. so how would you
do it for this case?

thanks
ts wrote:
> >>>>> "E" == Erich Lin <dmatrix00d@gmail.com> writes:
>
> E> just doing python example.py ? how?
> E> with backqutoes?
>
> no, no. just running interactively
>
> python example.py
>
> E> and cat assumes that this program will be run on *nix, which it may not
> E> be.
>
> yes, un*x system. Probably the command is `type' on a dos system, I don't know
>
> E> and even if python example.py | cat using what?
>
> on an un*x system if you execute
>
> python example.py | cat
>
> you'll wait 10 secondes before seeing the output 'hello' 'world' like for
> your ruby program.
>
> This is because I've created a pipe (`|') that you have this
> result. When you use #popen this is like if you create a pipe.
>
>
> --
>
> Guy Decoux

ts

6/30/2006 3:59:00 PM

0

>>>>> "E" == Erich Lin <dmatrix00d@gmail.com> writes:

E> i remember a post where someone said you can do this with popen, but it
E> won't work for all cases. i guess this is one of them. so how would you
E> do it for this case?

The only way to make it work is to modif example.py (or any program that
you want to run). If you can't do it, you will not be able to do what you
want.

--

Guy Decoux