-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathfibers.xml
More file actions
97 lines (89 loc) · 3.21 KB
/
fibers.xml
File metadata and controls
97 lines (89 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 376d3f9c2ef7fcd64d8b8503d552013acefb8b5b Maintainer: Rytia Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<chapter xml:id="language.fibers" xmlns="http://docbook.org/ns/docbook">
<title>纤程</title>
<simplesect xml:id="language.fibers.overview">
<title>纤程概述</title>
<?phpdoc print-version-for="fiber"?>
<para>
纤程(Fiber)表示一组有完整栈、可中断的功能。
纤程可以在调用堆栈中的任何位置被挂起,在纤程内暂停执行,直到稍后恢复。
</para>
<para>
纤程可以暂停整个执行堆栈,所以该函数的直接调用者不需要改变调用这个函数的方式。
</para>
<para>
你可以在调用堆栈的任意地方使用 <methodname>Fiber::suspend</methodname>
中断执行(也就是说,<methodname>Fiber::suspend</methodname>
的调用位置可以在一个深度嵌套的函数中,甚至可以不存在)。
</para>
<para>
与无栈的 <classname>Generator</classname> 不同, 每一个
<classname>Fiber</classname> 拥有自己的调用栈,并允许在一个深度前度的函数调用中将它们暂停。
声明了中断(interruption)点的函数(即调用 <methodname>Fiber::suspend</methodname>)
不需要改变自己的返回类型,不像使用 &yield; 一样需要返回一个
<classname>Generator</classname> 实例。
</para>
<para>
纤程可以在任意函数调用中被暂停,包括那些在 PHP VM 中被调用的函数。
例如被用于 <function>array_map</function> 的函数或者提供
<classname>Iterator</classname> 对象以被 &foreach; 调用的方法。
</para>
<para>
纤程一旦被暂停,可以使用 <methodname>Fiber::resume</methodname>
传递任意值、或者使用 <methodname>Fiber::throw</methodname>
向纤程抛出一个异常以恢复运行。这个值或者异常将会在
<methodname>Fiber::suspend</methodname> 中被返回(抛出)。
</para>
<note>
<simpara>
在 PHP 8.4.0 之前,不允许在对象<link linkend="language.oop5.decon.destructor">析构方法</link>执行期间切换纤程。
</simpara>
</note>
<example xml:id="language.fiber.example.basic"><!-- {{{ -->
<title>基础用法</title>
<programlisting role="php">
<![CDATA[
<?php
$fiber = new Fiber(function (): void {
$value = Fiber::suspend('fiber');
echo "Value used to resume fiber: ", $value, PHP_EOL;
});
$value = $fiber->start();
echo "Value from fiber suspending: ", $value, PHP_EOL;
$fiber->resume('test');
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Value from fiber suspending: fiber
Value used to resume fiber: test
]]>
</screen>
</example><!-- }}} -->
</simplesect>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->