simple threading

  • Thread starter Christoph Jasinski
  • Start date
C

Christoph Jasinski

[Note: parts of this message were removed to make it a legal post.]

Hi all,
I wanted to write a program that request stock quotes say every second. So I
started to write a thread like this:

require 'rubygems'
require 'yahoofinance'

thread=Thread.new do
YahooFinance::get_historical_quotes_days( 'goog', 10 ) do |row|
puts "YHOO,#{row.join(',')}"
end
end

When I run this I don't get any results. Which is really weird. Got all the
gems and so on.
But when I do the same code run in IRB it works! without any surprises. I
don't get it. Really.

Thanks for any help.

Chris
 
V

Vikhyat Korrapati

Hi all,
I wanted to write a program that request stock quotes say every second. S= o I
started to write a thread like this:

=A0require 'rubygems'
=A0require 'yahoofinance'

=A0thread=3DThread.new do
=A0 =A0YahooFinance::get_historical_quotes_days( 'goog', 10 ) do |row|
=A0 =A0 =A0puts "YHOO,#{row.join(',')}"
=A0 =A0end
=A0end

When I run this I don't get any results. Which is really weird. Got all t= he
gems and so on.
But when I do the same code run in IRB it works! without any surprises. I
don't get it. Really.

Thanks for any help.

Chris

Not sure, but adding 'thread.join' to the end might help.

--=20
Vikhyat Korrapati
http://aetus.net/
 
C

Christoph Jasinski

[Note: parts of this message were removed to make it a legal post.]

Thanks,
it works; I don't know why, but it works

Cheers,

Chris
 
J

James Gray

Thanks,
it works; I don't know why, but it works

I know why it work. :) Let me explain:

1. Your code splits into two threads (the main Thread Ruby started
your program in and the one you created to talk to YahooFinance in)
2. After you have the two Threads, they both begin working (trading
off time)
3. Your YahooFinance Thread probably starts talking over the network
4. However, your main Thread runs off the end of your file and when
that happens, Ruby exits (killing the unfinished YahooFinance Thread)

Thus, Vikhyat had you add a line that asked the main Thread to join()
or wait on the YahooFinance Thread to finish. That call won't return
until the second Thread ends, so Ruby will wait to exit.

Of course, that means the second Thread doesn't really do anything for
us in this case. But if you are launching a bunch of them or
continuing to work in the main Thread, there will be benefits.

Hope that helps.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top