Reputation: 1989
I have Batch file which will do tasks for me. I have to call this batch file from my PERL file.
I want this to be called Asynchronously. The task batch file will be doing will be time consuming and I don't want this Perl file to wait till the Bach file is over
This is what i tried for simulation
Batch file:
@ECHO off
ECHO ------Start of Batch file---------- >> C:\\temp\test.txt
Echo ------End of Batch file---------- >> C:\\temp\test.txt
pause
Reason I added Pause
is to simulate the aysnch. (Logically there if its Async there is no way to press enter in the batch file :-) )
My Perl code looks like this
system("CreateTicket.bat");
When I use system();
it is waiting for the batch file to complete its execution
I modified it with
"C://Work/Playground/Perl/CQ_createRTCTicket.bat"
; (with backticks Formatting problem here)
This also waits for the batch file to stop (I think because of Pause)
Or if my approach is wrong is there a way to simulate this async?
[I cant use any extra modules from CPAN]
Thanks,
Karthik
Upvotes: 0
Views: 3436
Reputation: 126
As you've noticed, system() waits for the other process. Internally, it forks() another process, that calls exec() to run your batch file and the original process waits. If you can't use CPAN then you can call fork() and exec() yourself and just not wait for the child process to finish.
Read these first:
Pay attention to the bit about signals and zombie processes. Once you've understood how it works it's simple enough.
If your batch file generates output you need to process in your perl then you can use open() to create a pipe to it (perldoc -f open).
Similar questions have been asked before:
What's the difference between Perl's backticks, system, and exec?
Upvotes: 1
Reputation: 38745
Use "start" (as proposed here
short POC:
print 'before', "\n";
system "start notepad.exe";
print 'between', "\n";
system "start wait.bat";
print 'after', "\n";
Upvotes: 1