Thoughts on loops and stacks

T

Todd A. Jacobs

I was thinking about some of the design issues in my dice.rb script:

http://www2.codegnome.org:59321/scripting/showscript.php?script=dice.rb

and one of the major issues is queueing. I'd like to ensure that
selecting multiple URLs doesn't overwrite the current contents from the
previous X selection, so I was thinking what I needed to do was
implement a FIFO stack.

My main issues are that I don't want to miss any X selections, but at
the same time I don't want ruby running in such a tight loop that it
sucks up excessive CPU time.

Right now, I'm doing:

loop do;
# grab X selection
# time-consuming WWW::Mechanize stuff with selection
sleep 0.3
end

which isn't really event driven. So, if I select several URLs in quick
succession, only the URL in the X selection *the next time ruby looks at
it* gets handled.

Suggestions?
 
R

Robert Klemme

I was thinking about some of the design issues in my dice.rb script:

http://www2.codegnome.org:59321/scripting/showscript.php?script=dice.rb

and one of the major issues is queueing. I'd like to ensure that
selecting multiple URLs doesn't overwrite the current contents from the
previous X selection, so I was thinking what I needed to do was
implement a FIFO stack.

There is no such thing as a "FIFO stack". A stack has LIFO semantics -
a queue FIFO.
My main issues are that I don't want to miss any X selections, but at
the same time I don't want ruby running in such a tight loop that it
sucks up excessive CPU time.

Right now, I'm doing:

loop do;
# grab X selection
# time-consuming WWW::Mechanize stuff with selection
sleep 0.3
end

which isn't really event driven. So, if I select several URLs in quick
succession, only the URL in the X selection *the next time ruby looks at
it* gets handled.

Suggestions?

If you have a single thread you can use an Array.

irb(main):022:0> q=[]
=> []
irb(main):023:0> 5.times {|i| q.push i}
=> 5
irb(main):024:0> until q.empty?; p q.shift; end
0
1
2
3
4
=> nil

Otherwise you can use a Queue as suggested by Ara. From your problem
description it seems you rather want a multithreaded solution.

Kind regards

robert
 

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

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,006
Latest member
MelinaLema

Latest Threads

Top