<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/refs.fileprocess.process.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'book.eio.php',
    1 => 'Eio',
    2 => 'Eio',
  ),
  'up' => 
  array (
    0 => 'refs.fileprocess.process.php',
    1 => '进程控制扩展',
  ),
  'prev' => 
  array (
    0 => 'refs.fileprocess.process.php',
    1 => '进程控制扩展',
  ),
  'next' => 
  array (
    0 => 'eio.setup.php',
    1 => '安装/配置',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/eio/book.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/book.eio.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="book.eio" class="book">
 
 <h1 class="title">Eio</h1>
 

 <div id="intro.eio" class="preface">
  <h1 class="title">简介</h1>
  <p class="simpara">
   This extension provides asyncronous POSIX I/O by means of <a href="http://software.schmorp.de/pkg/libeio.html" class="link external">&raquo;&nbsp;libeio</a> C
   library written by Marc Lehmann.
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此扩展在 Windows 平台上不可用。</span></p></blockquote>

  <p class="para">

  <div class="warning"><strong class="warning">警告</strong>
  <p class="simpara">
  It is important to be aware that each request is executed in a thread, and the
  order of execution of continuously queued requests basically is
  unpredictable. For instance, the following piece of code is incorrect.
  </p>
  </div>

  <div class="example" id="example-1">
  <p><strong>示例 #1 Incorrect requests</strong></p>
  <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Request to create symlink of $filename to $link<br /></span><span style="color: #0000BB">eio_symlink</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Request to move $filename to $new_filename<br /></span><span style="color: #0000BB">eio_rename</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$new_filename</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Process requests<br /></span><span style="color: #0000BB">eio_event_loop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   </div>

   In the example above <span class="function"><a href="function.eio-rename.php" class="function">eio_rename()</a></span> request may finish
   before <span class="function"><a href="function.eio-symlink.php" class="function">eio_symlink()</a></span>. To fix it you might call <span class="function"><a href="function.eio-rename.php" class="function">eio_rename()</a></span>
   in the callback of <span class="function"><a href="function.eio-symlink.php" class="function">eio_symlink()</a></span>:
  <div class="example" id="example-2">
   <p><strong>示例 #2 Calling request from a request callback</strong></p>
   <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">my_symlink_done</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// Request to move $filename to $new_filename<br /> </span><span style="color: #0000BB">eio_rename</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"/path/to/new-name"</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">// Process requests<br /> </span><span style="color: #0000BB">eio_event_loop</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #FF8000">// Request to create symlink of $filename to $link<br /></span><span style="color: #0000BB">eio_symlink</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #0000BB">EIO_PRI_DEFAULT</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_symlink_done"</span><span style="color: #007700">, </span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Process requests<br /></span><span style="color: #0000BB">eio_event_loop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  Alternatively, you might want to create a request group:

  <div class="example" id="example-3">
   <p><strong>示例 #3 Calling request from a request callback</strong></p>
   <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* Is called when the group requests are done */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">my_grp_done</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// ...<br /></span><span style="color: #007700">}<br /><br />function </span><span style="color: #0000BB">my_symlink_done</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// Create eio_rename request and add it to the group<br /> </span><span style="color: #0000BB">$req </span><span style="color: #007700">= </span><span style="color: #0000BB">eio_rename</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"/path/to/new-name"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">eio_grp_add</span><span style="color: #007700">(</span><span style="color: #0000BB">$grp</span><span style="color: #007700">, </span><span style="color: #0000BB">$req</span><span style="color: #007700">);<br /> </span><span style="color: #FF8000">// You might want to add more requests...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// Create a request group<br /></span><span style="color: #0000BB">$grp </span><span style="color: #007700">= </span><span style="color: #0000BB">eio_grp</span><span style="color: #007700">(</span><span style="color: #DD0000">"my_grp_done"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_grp_data"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Create eio_symlink request and add it to the group<br />// Pass $filename to the callback<br /></span><span style="color: #0000BB">$req </span><span style="color: #007700">= </span><span style="color: #0000BB">eio_symlink</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$link</span><span style="color: #007700">,<br />  </span><span style="color: #0000BB">EIO_PRI_DEFAULT</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_symlink_done"</span><span style="color: #007700">, </span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">eio_grp_add</span><span style="color: #007700">(</span><span style="color: #0000BB">$grp</span><span style="color: #007700">, </span><span style="color: #0000BB">$req</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Process requests<br /></span><span style="color: #0000BB">eio_event_loop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div></div>

  </div>

  Group is a special kind of request that could accumulate a set of regular
  <em>eio</em> requests. This could be used to create a complex
  request that opens, reads and closes a file.
  </p>
  <p class="para">
  Since version 0.3.0 alpha, a variable used in communications with libeio
  internally, could be retrieved with
  <span class="function"><a href="function.eio-get-event-stream.php" class="function">eio_get_event_stream()</a></span>. The variable could be used
  to bind to an event loop supported by some other extension. You might
  organize a simple event loop where eio and libevent work together:
  <div class="example" id="example-4">
   <p><strong>示例 #4 Using eio with libevent</strong></p>
   <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">my_eio_poll</span><span style="color: #007700">(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">, </span><span style="color: #0000BB">$events</span><span style="color: #007700">, </span><span style="color: #0000BB">$arg</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">/* Some libevent regulation might go here .. */<br />    </span><span style="color: #007700">if (</span><span style="color: #0000BB">eio_nreqs</span><span style="color: #007700">()) {<br />        </span><span style="color: #0000BB">eio_poll</span><span style="color: #007700">();<br />    }<br />    </span><span style="color: #FF8000">/* .. and here */<br /></span><span style="color: #007700">}<br /><br />function </span><span style="color: #0000BB">my_res_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">, </span><span style="color: #0000BB">$r</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">); </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$base </span><span style="color: #007700">= </span><span style="color: #0000BB">event_base_new</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$event </span><span style="color: #007700">= </span><span style="color: #0000BB">event_new</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// This stream is used to bind with libevent<br /></span><span style="color: #0000BB">$fd </span><span style="color: #007700">= </span><span style="color: #0000BB">eio_get_event_stream</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">eio_nop</span><span style="color: #007700">(</span><span style="color: #0000BB">EIO_PRI_DEFAULT</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_res_cb"</span><span style="color: #007700">, </span><span style="color: #DD0000">"nop data"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">eio_mkdir</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp/abc-eio-temp"</span><span style="color: #007700">, </span><span style="color: #0000BB">0750</span><span style="color: #007700">, </span><span style="color: #0000BB">EIO_PRI_DEFAULT</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_res_cb"</span><span style="color: #007700">, </span><span style="color: #DD0000">"mkdir data"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">/* some other eio_* calls here ... */<br /><br /><br />// set event flags<br /></span><span style="color: #0000BB">event_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$event</span><span style="color: #007700">, </span><span style="color: #0000BB">$fd</span><span style="color: #007700">, </span><span style="color: #0000BB">EV_READ </span><span style="color: #FF8000">/*| EV_PERSIST*/</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_eio_poll"</span><span style="color: #007700">, array(</span><span style="color: #0000BB">$event</span><span style="color: #007700">, </span><span style="color: #0000BB">$base</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// set event base<br /></span><span style="color: #0000BB">event_base_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$event</span><span style="color: #007700">, </span><span style="color: #0000BB">$base</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// enable event<br /></span><span style="color: #0000BB">event_add</span><span style="color: #007700">(</span><span style="color: #0000BB">$event</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// start event loop<br /></span><span style="color: #0000BB">event_base_loop</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* The same will be available via buffered libevent interface */<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div></div>

  </div>

  </p>
 </div>

 




 




 




 







<ul class="chunklist chunklist_book"><li><a href="eio.setup.php">安装/配置</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="eio.requirements.php">需求</a></li><li><a href="eio.installation.php">安装</a></li><li><a href="eio.resources.php">资源类型</a></li></ul></li><li><a href="eio.constants.php">预定义常量</a></li><li><a href="eio.examples.php">示例</a></li><li><a href="ref.eio.php">Eio 函数</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="function.eio-busy.php">eio_busy</a> — Artificially increase load. Could be useful in tests,
  benchmarking</li><li><a href="function.eio-cancel.php">eio_cancel</a> — Cancels a request</li><li><a href="function.eio-chmod.php">eio_chmod</a> — Change file/directory permissions</li><li><a href="function.eio-chown.php">eio_chown</a> — Change file/directory permissions</li><li><a href="function.eio-close.php">eio_close</a> — Close file</li><li><a href="function.eio-custom.php">eio_custom</a> — Execute custom request like any other eio_* call</li><li><a href="function.eio-dup2.php">eio_dup2</a> — Duplicate a file descriptor</li><li><a href="function.eio-event-loop.php">eio_event_loop</a> — Polls libeio until all requests proceeded</li><li><a href="function.eio-fallocate.php">eio_fallocate</a> — Allows the caller to directly manipulate the allocated disk
  space for a file</li><li><a href="function.eio-fchmod.php">eio_fchmod</a> — Change file permissions</li><li><a href="function.eio-fchown.php">eio_fchown</a> — Change file ownership</li><li><a href="function.eio-fdatasync.php">eio_fdatasync</a> — Synchronize a file's in-core state with storage device</li><li><a href="function.eio-fstat.php">eio_fstat</a> — Get file status</li><li><a href="function.eio-fstatvfs.php">eio_fstatvfs</a> — Get file system statistics</li><li><a href="function.eio-fsync.php">eio_fsync</a> — Synchronize a file's in-core state with storage device</li><li><a href="function.eio-ftruncate.php">eio_ftruncate</a> — Truncate a file</li><li><a href="function.eio-futime.php">eio_futime</a> — Change file last access and modification times</li><li><a href="function.eio-get-event-stream.php">eio_get_event_stream</a> — Get stream representing a variable used in internal communications with libeio</li><li><a href="function.eio-get-last-error.php">eio_get_last_error</a> — Returns string describing the last error associated with a request resource</li><li><a href="function.eio-grp.php">eio_grp</a> — Creates a request group</li><li><a href="function.eio-grp-add.php">eio_grp_add</a> — Adds a request to the request group</li><li><a href="function.eio-grp-cancel.php">eio_grp_cancel</a> — Cancels a request group</li><li><a href="function.eio-grp-limit.php">eio_grp_limit</a> — Set group limit</li><li><a href="function.eio-init.php">eio_init</a> — (Re-)initialize Eio</li><li><a href="function.eio-link.php">eio_link</a> — Create a hardlink for file</li><li><a href="function.eio-lstat.php">eio_lstat</a> — Get file status</li><li><a href="function.eio-mkdir.php">eio_mkdir</a> — Create directory</li><li><a href="function.eio-mknod.php">eio_mknod</a> — Create a special or ordinary file</li><li><a href="function.eio-nop.php">eio_nop</a> — Does nothing, except go through the whole request cycle</li><li><a href="function.eio-npending.php">eio_npending</a> — Returns number of finished, but unhandled requests</li><li><a href="function.eio-nready.php">eio_nready</a> — Returns number of not-yet handled requests</li><li><a href="function.eio-nreqs.php">eio_nreqs</a> — Returns number of requests to be processed</li><li><a href="function.eio-nthreads.php">eio_nthreads</a> — Returns number of threads currently in use</li><li><a href="function.eio-open.php">eio_open</a> — Opens a file</li><li><a href="function.eio-poll.php">eio_poll</a> — Can be to be called whenever there are pending requests that need finishing</li><li><a href="function.eio-read.php">eio_read</a> — Read from a file descriptor at given offset</li><li><a href="function.eio-readahead.php">eio_readahead</a> — Perform file readahead into page cache</li><li><a href="function.eio-readdir.php">eio_readdir</a> — Reads through a whole directory</li><li><a href="function.eio-readlink.php">eio_readlink</a> — Read value of a symbolic link</li><li><a href="function.eio-realpath.php">eio_realpath</a> — Get the canonicalized absolute pathname</li><li><a href="function.eio-rename.php">eio_rename</a> — Change the name or location of a file</li><li><a href="function.eio-rmdir.php">eio_rmdir</a> — Remove a directory</li><li><a href="function.eio-seek.php">eio_seek</a> — Seek to a position</li><li><a href="function.eio-sendfile.php">eio_sendfile</a> — Transfer data between file descriptors</li><li><a href="function.eio-set-max-idle.php">eio_set_max_idle</a> — Set maximum number of idle threads</li><li><a href="function.eio-set-max-parallel.php">eio_set_max_parallel</a> — Set maximum parallel threads</li><li><a href="function.eio-set-max-poll-reqs.php">eio_set_max_poll_reqs</a> — Set maximum number of requests processed in a poll</li><li><a href="function.eio-set-max-poll-time.php">eio_set_max_poll_time</a> — Set maximum poll time</li><li><a href="function.eio-set-min-parallel.php">eio_set_min_parallel</a> — Set minimum parallel thread number</li><li><a href="function.eio-stat.php">eio_stat</a> — Get file status</li><li><a href="function.eio-statvfs.php">eio_statvfs</a> — Get file system statistics</li><li><a href="function.eio-symlink.php">eio_symlink</a> — Create a symbolic link</li><li><a href="function.eio-sync.php">eio_sync</a> — Commit buffer cache to disk</li><li><a href="function.eio-sync-file-range.php">eio_sync_file_range</a> — Sync a file segment with disk</li><li><a href="function.eio-syncfs.php">eio_syncfs</a> — Calls Linux' syncfs syscall, if available</li><li><a href="function.eio-truncate.php">eio_truncate</a> — Truncate a file</li><li><a href="function.eio-unlink.php">eio_unlink</a> — Delete a name and possibly the file it refers to</li><li><a href="function.eio-utime.php">eio_utime</a> — Change file last access and modification times</li><li><a href="function.eio-write.php">eio_write</a> — Write to file</li></ul></li></ul></div><?php manual_footer($setup); ?>