Starting new process on Windows!

Is there an easy/clean way to start a new process in Windows that will
execute just a section of code? I’ve tried win32-process but it spawns
a
new version of the entire script which doesn’t help in my situation.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

Glen H. wrote:

Is there an easy/clean way to start a new process in Windows that will
execute just a section of code? I’ve tried win32-process but it spawns
a
new version of the entire script which doesn’t help in my situation.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

Try to use ruby threads. Example

thread = Thread.new do
Thread.stop #thread will not execute since you run it yourself
p “Hi I’m thread”
end

p “Before thread”
thread.run
p “After thread”

On Thu, Aug 14, 2008 at 8:38 AM, Mateusz T. [email protected]
wrote:

thread.run
p “After thread”

Posted via http://www.ruby-forum.com/.

Thanks, but I actually have the code in a thread already. I’m having an
issue where it appears that some combination of wx-ruby, win32ole, and
the
garbage collector are causing my app to hang. I was hoping to execute
the
win32ole code in a separate process in hopes of avoiding the hanging
problem.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On Thu, Aug 14, 2008 at 12:24 PM, Adam S. [email protected]
wrote:

done = false
excel.Quit();
data = 42 #initial_processing_here. data could be arbitrarily complex
done = true
end
end
end

Thanks guys, I appreciate it but it would appear the problem only occurs
on
the Windows Server 2003 machine I was testing on. It works fine in XP
which
is a bit strange.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On 8/14/08, Glen H. [email protected] wrote:

issue where it appears that some combination of wx-ruby, win32ole, and the
garbage collector are causing my app to hang. I was hoping to execute the
win32ole code in a separate process in hopes of avoiding the hanging
problem.

My first thought was you could make your own fork-like functionality.
But after I cooked up an example, I realized that the simpler thing
might be to just extract the section of code in question to a separate
script, and launch that in a new process with start separate_script.rb input_data
Just in case it’s useful, here’s my initial solution:
-Adam

require ‘win32ole’
Datafile = “fork.dump”
done = false
do_ole = ARGV.shift
data,result=0,0

def oleprocess data
excel = WIN32OLE.new(“excel.application”) #your ole app here!
workbook = excel.Workbooks.Add();
excel.Range(“a1”)[‘Value’] = data;
excel.Range(“a2”)[‘Value’] = “=a1*a1”;
result = excel.Range(“a2”)[‘Value’]
excel.ActiveWorkbook.Close(0);
excel.Quit();
result
end

if (do_ole == “-o”)
p “in new process”
File.open(Datafile,“rb”){|fp|data = Marshal.load(fp)}
result = oleprocess(data)
File.open(Datafile,“wb”){|fp|Marshal.dump(result,fp)}
exit
else
data = 42 #initial_processing_here. data could be arbitrarily
complex
File.open(Datafile,“wb”){|fp|Marshal.dump(data,fp)}
timestamp = File.mtime(Datafile)
p “launching new process”
system(“start ruby #{FILE} -o”)
until done
print ‘.’; sleep(0.1) #do gui stuff and other work here while you
are waiting…
if (File.mtime(Datafile) > timestamp) #other process is done
File.open(Datafile,“rb”){|fp|result = Marshal.load(fp)}
puts “\nsquare of #{data} is #{result}”
done = true
end
end
end

On 8/15/08, Glen H. [email protected] wrote:

On Thu, Aug 14, 2008 at 1:27 PM, Glen H. [email protected] wrote:

Actually the problem has re-surfaced. Basically what I need is fork
functionality. I have a small bit of code that I need to run as a separate
process. It would solve two problems if I could achieve this:

I will probably have to resort to system although I’d rather not wait for
the code to finish execution as the results don’t really matter to the main
thread of execution, and it takes a while.

On Windows, use:
system “start #{myapp}”

start spawns a new process then returns right away.

-Adam

On Thu, Aug 14, 2008 at 1:27 PM, Glen H. [email protected]
wrote:

spawns
the

excel.Range(“a2”)[‘Value’] = “=a1*a1”;
File.open(Datafile,“wb”){|fp|Marshal.dump(result,fp)}
if (File.mtime(Datafile) > timestamp) #other process is done
which
is a bit strange.


“Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

Actually the problem has re-surfaced. Basically what I need is fork
functionality. I have a small bit of code that I need to run as a
separate
process. It would solve two problems if I could achieve this:

  1. The code is a bit slow as it uses win32ole so the app wouldn’t appear
    to
    stall
  2. Separate heap/object space will hopefully fix the problem with the
    gui
    becoming totally unresponsive.

I will probably have to resort to system although I’d rather not wait
for
the code to finish execution as the results don’t really matter to the
main
thread of execution, and it takes a while.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

On Fri, Aug 15, 2008 at 12:06 PM, Adam S. [email protected]
wrote:

the code to finish execution as the results don’t really matter to the
-Adam

Awesome!!! Thanks Adam.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs