Starting new process on Windows!

G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

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.
 
M

Mateusz Tybura

Glen said:
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"
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

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"
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.
 
A

Adam Shelly

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.

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
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

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.

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
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.
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

Glen Holcomb 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.

Try to use ruby threads. Example
...

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.

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
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)

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.
 
A

Adam Shelly

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
 

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
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top