job.html

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

HTML
374
字号
</dd>
<dd>
<p>By default, the process is in the parent's process group (but in a new job).</p>
</dd>
</li>
<dt><strong><a name="item_no_window">no_window</a></strong>

<dd>
<p>A boolean; if true, the process is run without a console window. This flag is
only valid when starting a console application, otherwise it is ignored. If you
are launching a GUI application, use the <a href="#item_window_attr"><code>window_attr</code></a> tag instead.</p>
</dd>
<dd>
<p>By default, the process shares its parent's console.</p>
</dd>
</li>
<dt><strong><a name="item_stdin">stdin</a></strong>

<dd>
<p>An open input filehandle, or the name of an existing file. The resulting
filehandle will be used for the child's standard input handle.</p>
</dd>
<dd>
<p>By default, the child process shares the parent's standard input.</p>
</dd>
</li>
<dt><strong><a name="item_stdout">stdout</a></strong>

<dd>
<p>An open output filehandle or filename (will be opened for append). The
resulting filehandle will be used for the child's standard output handle.</p>
</dd>
<dd>
<p>By default, the child process shares the parent's standard output.</p>
</dd>
</li>
<dt><strong><a name="item_stderr">stderr</a></strong>

<dd>
<p>An open output filehandle or filename (will be opened for append). The
resulting filehandle will be used for the child's standard error handle.</p>
</dd>
<dd>
<p>By default, the child process shares the parent's standard error.</p>
</dd>
</li>
</dl>
<p>Unrecognized keys are ignored.</p>
</ol>
<li>
<p><code>run()</code></p>
<pre>
   <span class="variable">run</span><span class="operator">(</span><span class="variable">$timeout</span><span class="operator">,</span> <span class="variable">$which</span><span class="operator">);</span>
</pre>
<p>Provides a simple way to run the programs with a time limit. The
$timeout is in seconds with millisecond accuracy. This call blocks for
up to $timeout seconds, or until the processes finish.</p>
<p>The $which parameter specifies whether to wait for <em>all</em> processes to
complete within the $timeout, or whether to wait for <em>any</em> process to
complete. You should set this to a boolean, where a true value means to
wait for <em>all</em> the processes to complete, and a false value to wait
for <em>any</em>. If you do not specify $which, it defaults to true (<code>all</code>).</p>
<p>Returns a boolean indicating whether the processes exited by themselves,
or whether the time expired. A true return value means the processes
exited normally; a false value means one or more processes was killed
will $timeout.</p>
<p>You can get extended information on process exit codes using the
<code>status()</code> method.</p>
<p>For example, this is how to build two perl modules at the same time,
with a 5 minute timeout:</p>
<pre>
   <span class="keyword">use</span> <span class="variable">Win32::Job</span><span class="operator">;</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>
   <span class="variable">$job</span><span class="operator">-&gt;</span><span class="variable">spawn</span><span class="operator">(</span><span class="string">"cmd"</span><span class="operator">,</span> <span class="string">q{cmd /C "cd Mod1 &amp;&amp; nmake"}</span><span class="operator">);</span>
   <span class="variable">$job</span><span class="operator">-&gt;</span><span class="variable">spawn</span><span class="operator">(</span><span class="string">"cmd"</span><span class="operator">,</span> <span class="string">q{cmd /C "cd Mod2 &amp;&amp; nmake"}</span><span class="operator">);</span>
   <span class="variable">$ok</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">5</span> <span class="operator">*</span> <span class="number">60</span><span class="operator">);</span>
   <span class="keyword">print</span> <span class="string">"Mod1 and Mod2 built ok!\n"</span> <span class="keyword">if</span> <span class="variable">$ok</span><span class="operator">;</span>
</pre>
</li>
<li>
<p><code>watch()</code></p>
<pre>
   <span class="variable">watch</span><span class="operator">(\&amp;</span><span class="variable">handler</span><span class="operator">,</span> <span class="variable">$interval</span><span class="operator">,</span> <span class="variable">$which</span><span class="operator">);</span>
</pre>
<pre>
   <span class="variable">handler</span><span class="operator">(</span><span class="variable">$job</span><span class="operator">);</span>
</pre>
<p>Provides more fine-grained control over how to stop the programs.  You specify
a callback and an interval in seconds, and Win32::Job will call the &quot;watchdog&quot;
function at this interval, either until the processes finish or your watchdog
tells Win32::Job to stop. You must return a value indicating whether to stop: a
true value means to terminate the processes immediately.</p>
<p>The $which parameter has the same meaning as run()'s.</p>
<p>Returns a boolean with the same meaning as run()'s.</p>
<p>The handler may do anything it wants. One useful application of the <code>watch()</code>
method is to check the filesize of the output file, and terminate the Job if
the file becomes larger than a certain limit:</p>
<pre>
   <span class="keyword">use</span> <span class="variable">Win32::Job</span><span class="operator">;</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>
   <span class="variable">$job</span><span class="operator">-&gt;</span><span class="variable">spawn</span><span class="operator">(</span><span class="string">"cmd"</span><span class="operator">,</span> <span class="string">q{cmd /C "cd Mod1 &amp;&amp; nmake"}</span><span class="operator">,</span> <span class="operator">{</span>
       <span class="string">stdin</span>  <span class="operator">=&gt;</span> <span class="string">'NUL'</span><span class="operator">,</span> <span class="comment"># the NUL device</span>
       <span class="string">stdout</span> <span class="operator">=&gt;</span> <span class="string">'stdout.log'</span><span class="operator">,</span>
       <span class="string">stderr</span> <span class="operator">=&gt;</span> <span class="string">'stdout.log'</span><span class="operator">,</span>
   <span class="operator">}</span><span class="operator">);</span>
   <span class="variable">$ok</span> <span class="operator">=</span> <span class="variable">$job</span><span class="operator">-&gt;</span><span class="variable">watch</span><span class="operator">(</span><span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span>
       <span class="keyword">return</span> <span class="number">1</span> <span class="keyword">if</span> <span class="keyword">-s</span> <span class="string">"stdout.log"</span> <span class="operator">&gt;</span> <span class="number">1_000_000</span><span class="operator">;</span>
   <span class="operator">},</span> <span class="number">1</span><span class="operator">);</span>
   <span class="keyword">print</span> <span class="string">"Mod1 built ok!\n"</span> <span class="keyword">if</span> <span class="variable">$ok</span><span class="operator">;</span>
</pre>
</li>
<li>
<p><code>status()</code></p>
<pre>
   status()</pre>
<p>Returns a hash containing information about the processes in the job.
Only returns valid information <em>after</em> calling either <code>run()</code> or watch();
returns an empty hash if you have not yet called them. May be called from a
<code>watch()</code> callback, in which case the <a href="#item_exitcode"><code>exitcode</code></a> field should be ignored.</p>
<p>The keys of the hash are the process IDs; the values are a subhash
containing the following keys:</p>
<dl>
<dt><strong><a name="item_exitcode">exitcode</a></strong>

<dd>
<p>The exit code returned by the process. If the process was killed because
of a timeout, the value is 293.</p>
</dd>
</li>
<dt><strong><a name="item_time">time</a></strong>

<dd>
<p>The time accumulated by the process. This is yet another subhash containing
the subkeys (i) <code>user</code>, the amount of time the process spent in user
space; (ii) <code>kernel</code>, the amount of time the process spent in kernel space;
and (iii) <code>elapsed</code>, the total time the process was running.</p>
</dd>
</li>
</dl>
<li>
<p><a href="../../lib/Pod/perlfunc.html#item_kill"><code>kill()</code></a></p>
<pre>
   <span class="keyword">kill</span><span class="operator">();</span>
</pre>
<p>Kills all processes and subprocesses in the Job. Has no return value.
Sets the exit code to all processes killed to 293, which you can check
for in the <code>status()</code> return value.</p>
</li>
</ol>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p>For more information about jobs, see Microsoft's online help at</p>
<pre>
   <a href="http://msdn.microsoft.com/">http://msdn.microsoft.com/</a></pre>
<p>For other modules which do similar things (but not as well), see:</p>
<ol>
<li>
<p>Win32::Process</p>
<p>Low-level access to creating processes in Win32. See <a href="../../lib/Win32/Process.html">the Win32::Process manpage</a>.</p>
</li>
<li>
<p>Win32::Console</p>
<p>Low-level access to consoles in Win32. See <a href="../../lib/Win32/Console.html">the Win32::Console manpage</a>.</p>
</li>
<li>
<p>Win32::ProcFarm</p>
<p>Manage pools of threads to perform CPU-intensive tasks on Windows. See
<a href="../../Win32/ProcFarm.html">the Win32::ProcFarm manpage</a>.</p>
</li>
</ol>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>ActiveState (<a href="mailto:support@ActiveState.com">support@ActiveState.com</a>)</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>Copyright (c) 2002, ActiveState Corporation. All Rights Reserved.</p>

</body>

</html>

⌨️ 快捷键说明

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