job.html

来自「perl教程」· HTML 代码 · 共 374 行 · 第 1/2 页

HTML
374
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Win32::Job - Run sub-processes in a &quot;job&quot; environment</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>Win32::Job - Run sub-processes in a &quot;job&quot; environment</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#platforms">PLATFORMS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#using_win32__job">Using Win32::Job</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Win32::Job - Run sub-processes in a &quot;job&quot; environment</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
   <span class="keyword">use</span> <span class="variable">Win32::Job</span><span class="operator">;</span>
   
   <span class="keyword">my</span> <span class="variable">$job</span> <span class="operator">=</span> <span class="variable">Win32::Job</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">;</span>
</pre>
<pre>
   <span class="comment"># Run 'perl Makefile.PL' for 10 seconds</span>
   <span class="variable">$job</span><span class="operator">-&gt;</span><span class="variable">spawn</span><span class="operator">(</span><span class="variable">$Config</span><span class="operator">{</span><span class="string">perlpath</span><span class="operator">}</span><span class="operator">,</span> <span class="string">"perl Makefile.PL"</span><span class="operator">);</span>
   <span class="variable">$job</span><span class="operator">-&gt;</span><span class="variable">run</span><span class="operator">(</span><span class="number">10</span><span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="platforms">PLATFORMS</a></h1>
<p>Win32::Job requires Windows 2000 or later. Windows 95, 98, NT, and Me are not
supported.</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Windows 2000 introduced the concept of a &quot;job&quot;: a collection of processes
which can be controlled as a single unit. For example, you can reliably kill a
process and all of its children by launching the process in a job, then
telling Windows to kill all processes in the job.  Win32::Job makes this
feature available to Perl.</p>
<p>For example, imagine you want to allow 2 minutes for a process to complete.
If you have control over the child process, you can probably just run it in
the background, then poll every second to see if it has finished.</p>
<p>That's fine as long as the child process doesn't spawn any child processes.
What if it does? If you wrote the child process yourself and made an effort to
clean up your child processes before terminating, you don't have to worry.
If not, you will leave hanging processes (called &quot;zombie&quot; processes in Unix).</p>
<p>With Win32::Job, just create a new Job, then use the job to spawn the child
process. All <em>its</em> children will also be created in the new Job. When you
time out, just call the job's <a href="../../lib/Pod/perlfunc.html#item_kill"><code>kill()</code></a> method and the entire process tree will
be terminated.</p>
<p>
</p>
<hr />
<h1><a name="using_win32__job">Using Win32::Job</a></h1>
<p>The following methods are available:</p>
<ol>
<li>
<p><code>new()</code></p>
<pre>
   <span class="variable">new</span><span class="operator">();</span>
</pre>
<p>Creates a new Job object using the Win32 API call CreateJobObject(). The job
is created with a default security context, and is unnamed.</p>
<p>Note: this method returns <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> if <code>CreateJobObject()</code> fails. Look at <a href="../../lib/Pod/perlvar.html#item___e"><code>$^E</code></a>
for more detailed error information.</p>
</li>
<li>
<p><code>spawn()</code></p>
<pre>
   <span class="variable">spawn</span><span class="operator">(</span><span class="variable">$exe</span><span class="operator">,</span> <span class="variable">$args</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">%opts</span><span class="operator">);</span>
</pre>
<p>Creates a new process and associates it with the Job. The process is initially
suspended, and can be resumed with one of the other methods. Uses the Win32
API call CreateProcess(). Returns the PID of the newly created process.</p>
<p>Note: this method returns <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> if <code>CreateProcess()</code> fails. See <a href="../../lib/Pod/perlvar.html#item___e"><code>$^E</code></a> for
more detailed error information. One reason this will fail is if the calling
process is itself part of a job, and the job's security context does not allow
child processes to be created in a different job context than the parent.</p>
<p>The arguments are described here:</p>
<ol>
<li>
<p>$exe</p>
<p>The executable program to run. This may be <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>, in which case the first
argument in $args is the program to run.</p>
<p>If this has path information in it, it is used &quot;as is&quot; and passed to
CreateProcess(), which interprets it as either an absolute path, or a
path relative to the current drive and directory. If you did not specify an
extension, &quot;.exe&quot; is assumed.</p>
<p>If there are no path separators (either backslashes or forward slashes),
then Win32::Job will search the current directory and your PATH, looking
for the file. In addition, if you did not specify an extension, then
Win32::Job checks &quot;.exe&quot;, &quot;.com&quot;, and &quot;.bat&quot; in order. If it finds a &quot;.bat&quot;
file, Win32::Job will actually call <em>cmd.exe</em> and prepend &quot;cmd.exe&quot; to the
$args.</p>
<p>For example, assuming a fairly normal PATH:</p>
<pre>
   <span class="variable">spawn</span><span class="operator">(</span><span class="string">q{c:\winnt\system\cmd.exe}</span><span class="operator">,</span> <span class="string">q{cmd /C "echo %PATH%"}</span><span class="operator">)</span>
   <span class="variable">exefile</span><span class="operator">:</span> <span class="variable">c</span><span class="operator">:\</span><span class="variable">winnt</span><span class="operator">\</span><span class="keyword">system</span><span class="operator">\</span><span class="variable">cmd</span><span class="operator">.</span><span class="variable">exe</span>
   <span class="variable">cmdline</span><span class="operator">:</span> <span class="variable">cmd</span> <span class="operator">/</span><span class="variable">C</span> <span class="string">"echo %PATH%"</span>
</pre>
<pre>
   <span class="variable">spawn</span><span class="operator">(</span><span class="string">"cmd.exe"</span><span class="operator">,</span> <span class="string">q{cmd /C "echo %PATH%"}</span><span class="operator">)</span>
   <span class="variable">exefile</span><span class="operator">:</span> <span class="variable">c</span><span class="operator">:\</span><span class="variable">winnt</span><span class="operator">\</span><span class="keyword">system</span><span class="operator">\</span><span class="variable">cmd</span><span class="operator">.</span><span class="variable">exe</span>
   <span class="variable">cmdline</span><span class="operator">:</span> <span class="variable">cmd</span> <span class="operator">/</span><span class="variable">C</span> <span class="string">"echo %PATH%"</span>
</pre>
</li>
<li>
<p>$args</p>
<p>The commandline to pass to the executable program. The first word will be
<code>argv[0]</code> to an EXE file, so you should repeat the command name in $args.</p>
<p>For example:</p>
<pre>
   <span class="variable">$job</span><span class="operator">-&gt;</span><span class="variable">spawn</span><span class="operator">(</span><span class="variable">$Config</span><span class="operator">{</span><span class="string">perlpath</span><span class="operator">}</span><span class="operator">,</span> <span class="string">"perl foo.pl"</span><span class="operator">);</span>
</pre>
<p>In this case, the &quot;perl&quot; is ignored, since perl.exe doesn't use it.</p>
</li>
<li>
<p>%opts</p>
<p>A hash reference for advanced options. This parameter is optional.
the following keys are recognized:</p>
<dl>
<dt><strong><a name="item_cwd">cwd</a></strong>

<dd>
<p>A string specifying the current directory of the new process.</p>
</dd>
<dd>
<p>By default, the process shares the parent's current directory, <code>.</code>.</p>
</dd>
</li>
<dt><strong><a name="item_new_console">new_console</a></strong>

<dd>
<p>A boolean; if true, the process is started in a new console window.</p>
</dd>
<dd>
<p>By default, the process shares the parent's console. This has no effect on GUI
programs which do not interact with the console.</p>
</dd>
</li>
<dt><strong><a name="item_window_attr">window_attr</a></strong>

<dd>
<p>Either <code>minimized</code>, which displays the new window minimized; <code>maximimzed</code>,
which shows the new window maximized; or <code>hidden</code>, which does not display the
new window.</p>
</dd>
<dd>
<p>By default, the window is displayed using its application's defaults.</p>
</dd>
</li>
<dt><strong><a name="item_new_group">new_group</a></strong>

<dd>
<p>A boolean; if true, the process is the root of a new process group. This
process group includes all descendents of the child.</p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?