Sep
12

wxRuby application with Ruby threads

on September 12th, 2009 by admin

If you tried to run a wxRuby GUI application with other Ruby threads, you noticed that the wxRuby event loop is greedy, and renders the other threads very slow. Especially, if you do IO operations, file management in the other thread.

The problem is not wxRuby specific, you see the same behavior using other GUI toolkits – so I read it. The reason is that the Ruby thread scheduler can not interrupt external programs the same way it interrupts Ruby threads.

The suggested approach is to use a Wx::Timer in your main thread – the GUI thread – to periodically instruct the scheduler to activate other threads.

While various other sources suggest the following code

timer = Timer.new(self)
evt_timer(timer.id) do
  Thread.pass
end
timer.start(100)

it was not optimal in my case – still very slow.

I tried to run the timer with a 1ms interval

timer = Timer.new(self)
evt_timer(timer.id) do
  Thread.pass
end
timer.start(1)

still not good.

So – and here is what I suggest you do -, I ended up using a sleep in the timer event, and giving explicit, bigger time slots to my IO intensive thread.

timer = Timer.new(self)
evt_timer(timer.id) do
  sleep 0.01
end
timer.start(10)

The 0.01 (seconds sleep time) and the 10 (milliseconds interval) in my case are arbitrary numbers, you need to fine tune them based on your application and user experience. But, it helped big time for my application.

Related Posts

  • Ruby 1.9.1 character encoding, Regular Expressions and Ruby on Rails
  • A smart Winamp play-list - the implementation
  • Surprising element in design
  • Compiling Ruby with MinGW
  • Ruby 1.9 debugger
Tags: programming, ruby
english - RSS Feed

Leave a Reply

Click here to cancel reply.

CAPTCHA Image
Refresh Image

    Recent Posts

    • GPU with CUDA
    • First impressions of the Mac OS
    • Snow Leopard in VMWare
    • CentOS 5 under Windows Virtual PC
    • Turek Miklós esküvőjére
    • Have I told you lately that I like Windows 7?
    • Királynő a sakktáblán
    • TrueIP under Windows 7
    • Virtualdub capture headache
    • Endless Summer

    Archives

    • September 2010 (1)
    • August 2010 (7)
    • July 2010 (2)
    • June 2010 (1)
    • April 2010 (2)
    • March 2010 (1)
    • January 2010 (2)
    • December 2009 (3)
    • November 2009 (6)
    • October 2009 (6)
    • September 2009 (8)
    • August 2009 (14)

    Categories

    • english (37)
    • magyar (16)

    életmód autó cloud fényképészet food gondolatok hírek humor ideas lesson learnt lifestyle logic logika matematika math mese movie music photography product review programming ruby science social software történelem television thoughts travel tudomány utazás video web zene

    WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.

    Blogroll

    • James Chik
    • Klement Vilmos

    Links

    • Home page
    • Photography

    Meta

    • Log in
Blog software by WordPress
Theme based on blue-fade by Web Considerations, LLC
Entries (RSS) and Comments (RSS)
36 queries. 0.322