Sunday 15 July 2012

Ruby script hangs forever -



Ruby script hangs forever -

this little script supposed generate user-specified amount of random numbers , print them. it's multithreaded script , think that's problem lies. i'm not getting errors, when run script hangs.

num = [] while 0.upto argv[0].to_i num << rand{254} end current_index = 0 while current_index < num.size chunk = num[current_index, 5] threads = [] chunk.each |n| threads << thread.new puts n end end threads.each |thread| thread.join end current_index += chunk.size end

you cannot utilize while loop upto.

change to:

0.upto argv[0].to_i num << rand(254) end

and works (i've changed braces curly one, because believe want 254 parameter here).

sidenote:

remember when writing threads programme in ruby, cruby has gil - global interpreter lock. hence 1 thread operating @ 1 time. if want different behaviour - switch illustration jruby. more info gil can found f.e. here: http://www.jstorimer.com/blogs/workingwithcode/8085491-nobody-understands-the-gil

ruby

No comments:

Post a Comment