• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Need help on writing threaded plugin code

Scheduled Pinned Locked Moved Developers' Forum
10 Posts 4 Posters 889 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    RichMorin
    last edited by 30 Nov 2010, 13:26

    I've looked around for information on writing threaded plugin code,
    but I'm not having much luck. (Of course, part of the problem has to do with the overloading of the words "thread" and threaded". <g>) Does anyone here know the ropes?

    I have a plugin that needs to scan a directory for the presence of new files, processing and removing them when they appear. Something like this:

    
    loop do             # Loop forever, handling files.
      paths = Dir.glob(patt)
    
      if paths.empty?   # Nothing to do; zzz...
        sleep(0.05)
      else              # Process incoming messages.
        paths.each do |path|
          process_input(path)
          break if @terminate_bd
        end
      end
      break if @terminate_bd
    end
    
    

    If I run this code in my plugin, it works just fine. Unfortunately, it also makes SketchUp unresponsive to other input, keeps the Ruby Console from updating, etc. So, I tried giving the code its own Thread:

    
    Thread.new do
      loop do             # Loop forever, handling files.
        ...
      end
    end
    
    

    This doesn't tie up SketchUp, but it doesn't work, either. In fact, it appears that the code in the thread never gets run at all. Comments? Clues? Suggestions?

    -r

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 30 Nov 2010, 14:00

      All the previous threads ... (no pun intended) on SketchUp Ruby threads I've seen has said that it doesn't work. Pain and suffering is the general jest of it AFIK. (I've not tried it myself.)

      You should get some threads πŸ˜’ if you search the archive.

      Thomas Thomassen β€” SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • A Offline
        AdamB
        last edited by 30 Nov 2010, 15:55

        Specifically, Ruby Threads are "Green threads". Think of it as an "emulated" thread that Ruby creates internally by having its own mini-scheduler like an OS has but here sharing out itself. So Ruby threads can never be multicore for example.

        Also you need to be clear why you want to thread your task. If your task doesn't actually have external blocking situations (waiting for something to be ready), you will never gain any performance by threading. In fact you more likely will reduce performance.

        Lastly, if you use "real" threads, you need to ensure that the APIs you use from those threads is "thread safe" - meaning it can deal with multiple threads calling into it. AFAIK SketchUp API is not thread safe.

        Adam

        Developer of LightUp Click for website

        1 Reply Last reply Reply Quote 0
        • R Offline
          RichMorin
          last edited by 30 Nov 2010, 17:36

          I have two reasons for thinking that I might need to use threads (or some other solution). Even in the context of show_modal(), it's annoying to have the Ruby Console (etc.) stalled. In the context of show(), it's unacceptable to have the rest of SketchUp stalled.

          I'm not married to the idea of threads, but I need some way to detect and process incoming data files. Suggestions?

          1 Reply Last reply Reply Quote 0
          • F Offline
            fredo6
            last edited by 30 Nov 2010, 18:16

            @richmorin said:

            I have two reasons for thinking that I might need to use threads (or some other solution). Even in the context of show_modal(), it's annoying to have the Ruby Console (etc.) stalled. In the context of show(), it's unacceptable to have the rest of SketchUp stalled.

            I'm not married to the idea of threads, but I need some way to detect and process incoming data files. Suggestions?

            Did you try with a timer in JavaScript. It seems that the Browser control window runs in its own thread?

            Fredo

            1 Reply Last reply Reply Quote 0
            • T Offline
              thomthom
              last edited by 30 Nov 2010, 18:55

              @unknownuser said:

              It seems that the Browser control window runs in its own thread?

              hmmm.... not too sure about that. If you open a WebDialog and do some heavy processing even the webdialogs stall.

              Thomas Thomassen β€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • R Offline
                RichMorin
                last edited by 30 Nov 2010, 20:16

                FWIW, I found a different solution to the problem. Instead of looping, I set up a timer which fires every 0.05 seconds. The associated block looks for files, etc. Seems to work just dandy...

                1 Reply Last reply Reply Quote 0
                • A Offline
                  AdamB
                  last edited by 30 Nov 2010, 20:31

                  @richmorin said:

                  Even in the context of show_modal(), it's annoying to have the Ruby Console (etc.) stalled. In the context of show(), it's unacceptable to have the rest of SketchUp stalled.

                  Non-modal dialogs don't block SketchUp. Yer doing it wrong. πŸ˜„

                  Developer of LightUp Click for website

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    thomthom
                    last edited by 30 Nov 2010, 21:28

                    @richmorin said:

                    Instead of looping, I set up a timer which fires every 0.05 seconds. The associated block looks for files, etc. Seems to work just dandy...

                    UI.timer ? or a Javascript timer?
                    Remember that UI.timer is bugged in pre-SU8. In those versions floating numbers are rounded down - so 0.05 would becomes 0.0.

                    Thomas Thomassen β€” SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • R Offline
                      RichMorin
                      last edited by 2 Dec 2010, 19:33

                      @thomthom said:

                      UI.timer ? or a Javascript timer?

                      This has to be a UI.timer, because it runs in the plugin (telling the plugin when to look for proxied messages from the JavaScript client code).

                      I'm using SU 8, so the bug you mention isn't a problem for me. However, it would be nice to have a solution that works for earlier versions.

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      1 / 1
                      • First post
                        1/10
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement