polyphony 0.43.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitbook.yaml +4 -0
- data/.github/workflows/test.yml +29 -0
- data/.gitignore +59 -0
- data/.rubocop.yml +175 -0
- data/CHANGELOG.md +393 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +141 -0
- data/LICENSE +21 -0
- data/README.md +51 -0
- data/Rakefile +26 -0
- data/TODO.md +201 -0
- data/bin/polyphony-debug +87 -0
- data/docs/_config.yml +64 -0
- data/docs/_includes/head.html +40 -0
- data/docs/_includes/title.html +1 -0
- data/docs/_sass/custom/custom.scss +10 -0
- data/docs/_sass/overrides.scss +0 -0
- data/docs/_user-guide/all-about-timers.md +126 -0
- data/docs/_user-guide/index.md +9 -0
- data/docs/_user-guide/web-server.md +136 -0
- data/docs/api-reference/exception.md +27 -0
- data/docs/api-reference/fiber.md +425 -0
- data/docs/api-reference/index.md +9 -0
- data/docs/api-reference/io.md +36 -0
- data/docs/api-reference/object.md +99 -0
- data/docs/api-reference/polyphony-baseexception.md +33 -0
- data/docs/api-reference/polyphony-cancel.md +26 -0
- data/docs/api-reference/polyphony-moveon.md +24 -0
- data/docs/api-reference/polyphony-net.md +20 -0
- data/docs/api-reference/polyphony-process.md +28 -0
- data/docs/api-reference/polyphony-resourcepool.md +59 -0
- data/docs/api-reference/polyphony-restart.md +18 -0
- data/docs/api-reference/polyphony-terminate.md +18 -0
- data/docs/api-reference/polyphony-threadpool.md +67 -0
- data/docs/api-reference/polyphony-throttler.md +77 -0
- data/docs/api-reference/polyphony.md +36 -0
- data/docs/api-reference/thread.md +88 -0
- data/docs/assets/img/echo-fibers.svg +1 -0
- data/docs/assets/img/sleeping-fiber.svg +1 -0
- data/docs/faq.md +195 -0
- data/docs/favicon.ico +0 -0
- data/docs/getting-started/index.md +10 -0
- data/docs/getting-started/installing.md +34 -0
- data/docs/getting-started/overview.md +486 -0
- data/docs/getting-started/tutorial.md +359 -0
- data/docs/index.md +94 -0
- data/docs/main-concepts/concurrency.md +151 -0
- data/docs/main-concepts/design-principles.md +161 -0
- data/docs/main-concepts/exception-handling.md +291 -0
- data/docs/main-concepts/extending.md +89 -0
- data/docs/main-concepts/fiber-scheduling.md +197 -0
- data/docs/main-concepts/index.md +9 -0
- data/docs/polyphony-logo.png +0 -0
- data/examples/adapters/concurrent-ruby.rb +9 -0
- data/examples/adapters/pg_client.rb +36 -0
- data/examples/adapters/pg_notify.rb +35 -0
- data/examples/adapters/pg_pool.rb +43 -0
- data/examples/adapters/pg_transaction.rb +31 -0
- data/examples/adapters/redis_blpop.rb +12 -0
- data/examples/adapters/redis_channels.rb +122 -0
- data/examples/adapters/redis_client.rb +19 -0
- data/examples/adapters/redis_pubsub.rb +26 -0
- data/examples/adapters/redis_pubsub_perf.rb +68 -0
- data/examples/core/01-spinning-up-fibers.rb +18 -0
- data/examples/core/02-awaiting-fibers.rb +20 -0
- data/examples/core/03-interrupting.rb +39 -0
- data/examples/core/04-handling-signals.rb +19 -0
- data/examples/core/xx-agent.rb +102 -0
- data/examples/core/xx-at_exit.rb +29 -0
- data/examples/core/xx-caller.rb +12 -0
- data/examples/core/xx-channels.rb +45 -0
- data/examples/core/xx-daemon.rb +14 -0
- data/examples/core/xx-deadlock.rb +8 -0
- data/examples/core/xx-deferring-an-operation.rb +14 -0
- data/examples/core/xx-erlang-style-genserver.rb +81 -0
- data/examples/core/xx-exception-backtrace.rb +40 -0
- data/examples/core/xx-fork-cleanup.rb +22 -0
- data/examples/core/xx-fork-spin.rb +42 -0
- data/examples/core/xx-fork-terminate.rb +27 -0
- data/examples/core/xx-forking.rb +24 -0
- data/examples/core/xx-move_on.rb +23 -0
- data/examples/core/xx-pingpong.rb +18 -0
- data/examples/core/xx-queue-async.rb +120 -0
- data/examples/core/xx-readpartial.rb +18 -0
- data/examples/core/xx-recurrent-timer.rb +12 -0
- data/examples/core/xx-resource_delegate.rb +31 -0
- data/examples/core/xx-signals.rb +16 -0
- data/examples/core/xx-sleep-forever.rb +9 -0
- data/examples/core/xx-sleeping.rb +25 -0
- data/examples/core/xx-snooze-starve.rb +16 -0
- data/examples/core/xx-spin-fork.rb +49 -0
- data/examples/core/xx-spin_error_backtrace.rb +33 -0
- data/examples/core/xx-state-machine.rb +51 -0
- data/examples/core/xx-stop.rb +20 -0
- data/examples/core/xx-supervise-process.rb +30 -0
- data/examples/core/xx-supervisors.rb +21 -0
- data/examples/core/xx-thread-selector-sleep.rb +51 -0
- data/examples/core/xx-thread-selector-snooze.rb +46 -0
- data/examples/core/xx-thread-sleep.rb +17 -0
- data/examples/core/xx-thread-snooze.rb +34 -0
- data/examples/core/xx-thread_pool.rb +17 -0
- data/examples/core/xx-throttling.rb +18 -0
- data/examples/core/xx-timeout.rb +10 -0
- data/examples/core/xx-timer-gc.rb +17 -0
- data/examples/core/xx-trace.rb +79 -0
- data/examples/core/xx-using-a-mutex.rb +21 -0
- data/examples/core/xx-worker-thread.rb +30 -0
- data/examples/io/tunnel.rb +48 -0
- data/examples/io/xx-backticks.rb +11 -0
- data/examples/io/xx-echo_client.rb +25 -0
- data/examples/io/xx-echo_client_from_stdin.rb +21 -0
- data/examples/io/xx-echo_pipe.rb +16 -0
- data/examples/io/xx-echo_server.rb +17 -0
- data/examples/io/xx-echo_server_with_timeout.rb +34 -0
- data/examples/io/xx-echo_stdin.rb +14 -0
- data/examples/io/xx-happy-eyeballs.rb +36 -0
- data/examples/io/xx-httparty.rb +38 -0
- data/examples/io/xx-irb.rb +17 -0
- data/examples/io/xx-net-http.rb +15 -0
- data/examples/io/xx-open.rb +16 -0
- data/examples/io/xx-switch.rb +15 -0
- data/examples/io/xx-system.rb +11 -0
- data/examples/io/xx-tcpserver.rb +15 -0
- data/examples/io/xx-tcpsocket.rb +18 -0
- data/examples/io/xx-zip.rb +19 -0
- data/examples/performance/fiber_transfer.rb +47 -0
- data/examples/performance/fs_read.rb +38 -0
- data/examples/performance/mem-usage.rb +56 -0
- data/examples/performance/messaging.rb +29 -0
- data/examples/performance/multi_snooze.rb +33 -0
- data/examples/performance/snooze.rb +39 -0
- data/examples/performance/snooze_raw.rb +39 -0
- data/examples/performance/thread-vs-fiber/polyphony_mt_server.rb +74 -0
- data/examples/performance/thread-vs-fiber/polyphony_server.rb +45 -0
- data/examples/performance/thread-vs-fiber/polyphony_server_read_loop.rb +58 -0
- data/examples/performance/thread-vs-fiber/threaded_server.rb +27 -0
- data/examples/performance/thread-vs-fiber/xx-httparty_multi.rb +36 -0
- data/examples/performance/thread-vs-fiber/xx-httparty_threaded.rb +29 -0
- data/examples/performance/thread_pool_perf.rb +63 -0
- data/examples/performance/xx-array.rb +11 -0
- data/examples/performance/xx-fiber-switch.rb +9 -0
- data/examples/performance/xx-snooze.rb +15 -0
- data/examples/xx-spin.rb +32 -0
- data/ext/libev/Changes +548 -0
- data/ext/libev/LICENSE +37 -0
- data/ext/libev/README +59 -0
- data/ext/libev/README.embed +3 -0
- data/ext/libev/ev.c +5279 -0
- data/ext/libev/ev.h +856 -0
- data/ext/libev/ev_epoll.c +296 -0
- data/ext/libev/ev_kqueue.c +224 -0
- data/ext/libev/ev_linuxaio.c +642 -0
- data/ext/libev/ev_poll.c +156 -0
- data/ext/libev/ev_port.c +192 -0
- data/ext/libev/ev_select.c +316 -0
- data/ext/libev/ev_vars.h +215 -0
- data/ext/libev/ev_win32.c +162 -0
- data/ext/libev/ev_wrap.h +216 -0
- data/ext/libev/test_libev_win32.c +123 -0
- data/ext/polyphony/extconf.rb +20 -0
- data/ext/polyphony/fiber.c +109 -0
- data/ext/polyphony/libev.c +2 -0
- data/ext/polyphony/libev.h +9 -0
- data/ext/polyphony/libev_agent.c +882 -0
- data/ext/polyphony/polyphony.c +71 -0
- data/ext/polyphony/polyphony.h +97 -0
- data/ext/polyphony/polyphony_ext.c +21 -0
- data/ext/polyphony/queue.c +168 -0
- data/ext/polyphony/ring_buffer.c +96 -0
- data/ext/polyphony/ring_buffer.h +28 -0
- data/ext/polyphony/thread.c +208 -0
- data/ext/polyphony/tracing.c +11 -0
- data/lib/polyphony.rb +136 -0
- data/lib/polyphony/adapters/fs.rb +19 -0
- data/lib/polyphony/adapters/irb.rb +52 -0
- data/lib/polyphony/adapters/postgres.rb +110 -0
- data/lib/polyphony/adapters/process.rb +33 -0
- data/lib/polyphony/adapters/redis.rb +67 -0
- data/lib/polyphony/adapters/trace.rb +138 -0
- data/lib/polyphony/core/channel.rb +46 -0
- data/lib/polyphony/core/exceptions.rb +36 -0
- data/lib/polyphony/core/global_api.rb +124 -0
- data/lib/polyphony/core/resource_pool.rb +117 -0
- data/lib/polyphony/core/sync.rb +21 -0
- data/lib/polyphony/core/thread_pool.rb +64 -0
- data/lib/polyphony/core/throttler.rb +41 -0
- data/lib/polyphony/event.rb +17 -0
- data/lib/polyphony/extensions/core.rb +174 -0
- data/lib/polyphony/extensions/fiber.rb +379 -0
- data/lib/polyphony/extensions/io.rb +221 -0
- data/lib/polyphony/extensions/openssl.rb +81 -0
- data/lib/polyphony/extensions/socket.rb +150 -0
- data/lib/polyphony/extensions/thread.rb +108 -0
- data/lib/polyphony/net.rb +77 -0
- data/lib/polyphony/version.rb +5 -0
- data/polyphony.gemspec +40 -0
- data/test/coverage.rb +54 -0
- data/test/eg.rb +27 -0
- data/test/helper.rb +56 -0
- data/test/q.rb +24 -0
- data/test/run.rb +5 -0
- data/test/stress.rb +25 -0
- data/test/test_agent.rb +130 -0
- data/test/test_event.rb +59 -0
- data/test/test_ext.rb +196 -0
- data/test/test_fiber.rb +988 -0
- data/test/test_global_api.rb +352 -0
- data/test/test_io.rb +249 -0
- data/test/test_kernel.rb +57 -0
- data/test/test_process_supervision.rb +46 -0
- data/test/test_queue.rb +112 -0
- data/test/test_resource_pool.rb +138 -0
- data/test/test_signal.rb +100 -0
- data/test/test_socket.rb +34 -0
- data/test/test_supervise.rb +103 -0
- data/test/test_thread.rb +170 -0
- data/test/test_thread_pool.rb +101 -0
- data/test/test_throttler.rb +50 -0
- data/test/test_trace.rb +68 -0
- metadata +482 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
---
|
2
|
+
layout: page
|
3
|
+
title: ::Exception
|
4
|
+
parent: API Reference
|
5
|
+
permalink: /api-reference/exception/
|
6
|
+
---
|
7
|
+
# ::Exception
|
8
|
+
|
9
|
+
[Ruby core Exception documentation](https://ruby-doc.org/core-2.7.0/Exception.html)
|
10
|
+
|
11
|
+
The core `Exception` class is enhanced to provide a better backtrace that takes
|
12
|
+
into account the fiber hierarchy. In addition, a `source_fiber` attribute allows
|
13
|
+
tracking the fiber from which an uncaught exception was propagated.
|
14
|
+
|
15
|
+
## Class Methods
|
16
|
+
|
17
|
+
## Instance methods
|
18
|
+
|
19
|
+
### #source_fiber → fiber
|
20
|
+
|
21
|
+
Returns the fiber in which the exception occurred. Polyphony sets this attribute
|
22
|
+
only for uncaught exceptions. Currently this attribute is only used in a
|
23
|
+
meaningful way for supervising fibers.
|
24
|
+
|
25
|
+
### #source_fiber=(fiber) → fiber
|
26
|
+
|
27
|
+
Sets the fiber in which the exception occurred.
|
@@ -0,0 +1,425 @@
|
|
1
|
+
---
|
2
|
+
layout: page
|
3
|
+
title: ::Fiber
|
4
|
+
parent: API Reference
|
5
|
+
permalink: /api-reference/fiber/
|
6
|
+
# prev_title: Tutorial
|
7
|
+
# next_title: How Fibers are Scheduled
|
8
|
+
---
|
9
|
+
# ::Fiber
|
10
|
+
|
11
|
+
[Ruby core Fiber documentation](https://ruby-doc.org/core-2.7.0/Fiber.html)
|
12
|
+
|
13
|
+
Polyphony enhances the core `Fiber` class with APIs for scheduling, exception
|
14
|
+
handling, message passing, and more. Normally, fibers should be created using
|
15
|
+
`Object#spin` or `Fiber#spin`, but some fibers might be created implicitly when
|
16
|
+
using lazy enumerators or third party gems. For fibers created implicitly,
|
17
|
+
Polyphony provides `Fiber#setup_raw`, which enables scheduling and message
|
18
|
+
passing for such fibers.
|
19
|
+
|
20
|
+
While most work on fibers since their introduction into MRI Ruby has
|
21
|
+
concentrated on `Fiber#resume` and `Fiber.yield` for transferring control
|
22
|
+
between fibers, Polyphony uses `Fiber#transfer` exclusively, which allows
|
23
|
+
fully symmetric transfer of control between fibers.
|
24
|
+
|
25
|
+
## Class Methods
|
26
|
+
|
27
|
+
### ::await(\*fibers) → [\*result]
|
28
|
+
|
29
|
+
Awaits all given fibers, returning when all fibers have terminated. If one of
|
30
|
+
the given fibers terminates with an uncaught exception, `::await` will terminate
|
31
|
+
and await all fibers that are still running, then reraise the exception. All the
|
32
|
+
given fibers are guaranteed to have terminated when `::await` returns. If no
|
33
|
+
exception is raised, `::await` returns an array containing the results of all
|
34
|
+
given fibers, in the same order.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
f1 = spin { sleep 1 }
|
38
|
+
f2 = spin { sleep 2 }
|
39
|
+
Fiber.await(f1, f2)
|
40
|
+
```
|
41
|
+
|
42
|
+
### ::select(\*fibers) → [\*result]
|
43
|
+
|
44
|
+
Selects the first fiber to have terminated among the given fibers. If any of the
|
45
|
+
given fibers terminates with an uncaught exception, `::select` will reraise the
|
46
|
+
exception. If no exception is raised, `::select` returns an array containing the
|
47
|
+
fiber and its result. All given fibers are guaranteed to have terminated when
|
48
|
+
`::select` returns.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
# get the fastest reply of a bunch of URLs
|
52
|
+
fibers = urls.map { |u| spin { [u, HTTParty.get(u)] } }
|
53
|
+
fiber, (url, result) = Fiber.select(*fibers)
|
54
|
+
```
|
55
|
+
|
56
|
+
## Instance methods
|
57
|
+
|
58
|
+
### #<<(object) → fiber<br>#send(object) → fiber
|
59
|
+
|
60
|
+
Adds a message to the fiber's mailbox. The message can be any object. This
|
61
|
+
method is complemented by `Fiber#receive`.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
f = spin do
|
65
|
+
loop do
|
66
|
+
receiver, x = receive
|
67
|
+
receiver << x * 10
|
68
|
+
end
|
69
|
+
end
|
70
|
+
f << 2
|
71
|
+
result = receive #=> 20
|
72
|
+
```
|
73
|
+
|
74
|
+
### #auto_watcher → async
|
75
|
+
|
76
|
+
Returns a reusable `Gyro::Async` watcher instance associated with the fiber.
|
77
|
+
This method provides a way to minimize watcher allocation. Instead of allocating
|
78
|
+
a new async watcher every time one is needed, the same watcher associated with
|
79
|
+
the fiber is reused.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
def work(async)
|
83
|
+
do_something
|
84
|
+
async.signal
|
85
|
+
end
|
86
|
+
|
87
|
+
async = Fiber.current.auto_watcher
|
88
|
+
spin { work(async) }
|
89
|
+
async.await
|
90
|
+
```
|
91
|
+
|
92
|
+
### #await → object<br>#join → object
|
93
|
+
|
94
|
+
Awaits the termination of the fiber. If the fiber terminates with an uncaught
|
95
|
+
exception, `#await` will reraise the exception. Otherwise `#await` returns the
|
96
|
+
result of the fiber.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
f = spin { 'foo' }
|
100
|
+
f.await #=> 'foo'
|
101
|
+
```
|
102
|
+
|
103
|
+
### #await_all_children → fiber
|
104
|
+
|
105
|
+
Waits for all the fiber's child fibers to terminate. This method is normally
|
106
|
+
coupled with `Fiber#terminate_all_children`. See also
|
107
|
+
`Fiber#shutdown_all_children`.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
jobs.each { |j| spin { process(j) } }
|
111
|
+
sleep 1
|
112
|
+
terminate_all_children
|
113
|
+
await_all_children
|
114
|
+
```
|
115
|
+
|
116
|
+
### #caller → [*location]
|
117
|
+
|
118
|
+
Return the execution stack of the fiber, including that of the fiber from which
|
119
|
+
it was spun.
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
spin {
|
123
|
+
spin {
|
124
|
+
spin {
|
125
|
+
pp Fiber.current.caller
|
126
|
+
}.await
|
127
|
+
}.await
|
128
|
+
}.await
|
129
|
+
|
130
|
+
#=> ["examples/core/xx-caller.rb:3:in `block (2 levels) in <main>'",
|
131
|
+
#=> "examples/core/xx-caller.rb:2:in `block in <main>'",
|
132
|
+
#=> "examples/core/xx-caller.rb:1:in `<main>'"]
|
133
|
+
```
|
134
|
+
|
135
|
+
### #cancel! → fiber
|
136
|
+
|
137
|
+
Terminates the fiber by raising a `Polyphony::Cancel` exception. If uncaught,
|
138
|
+
the exception will be propagated.
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
f = spin { sleep 1 }
|
142
|
+
f.cancel! #=> exception: Polyphony::Cancel
|
143
|
+
```
|
144
|
+
|
145
|
+
### #children → [\*fiber]
|
146
|
+
|
147
|
+
Returns an array containing all of the fiber's child fibers. A child fiber's
|
148
|
+
lifetime is limited to that of its immediate parent.
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
f1 = spin { sleep 1 }
|
152
|
+
f2 = spin { sleep 1 }
|
153
|
+
f3 = spin { sleep 1 }
|
154
|
+
Fiber.current.children #=> [f1, f2, f3]
|
155
|
+
```
|
156
|
+
|
157
|
+
### #interrupt(object = nil) → fiber<br>#stop(object = nil) → fiber
|
158
|
+
|
159
|
+
Terminates the fiber by raising a `Polyphony::MoveOn` exception in its context.
|
160
|
+
The given object will be set as the fiber's result. Note that a `MoveOn`
|
161
|
+
exception is never propagated.
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
f = spin { do_something_slow }
|
165
|
+
f.interrupt('never mind')
|
166
|
+
f.await #=> 'never mind'
|
167
|
+
```
|
168
|
+
|
169
|
+
### #location → string
|
170
|
+
|
171
|
+
Returns the location of the fiber's associated block, or `(root)` for the main
|
172
|
+
fiber.
|
173
|
+
|
174
|
+
### #main? → true or false
|
175
|
+
|
176
|
+
Returns true if the fiber is the main fiber for its thread.
|
177
|
+
|
178
|
+
### #parent → fiber
|
179
|
+
|
180
|
+
Returns the fiber's parent fiber. The main fiber's parent is nil.
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
f = spin { sleep 1 }
|
184
|
+
f.parent #=> Fiber.current
|
185
|
+
```
|
186
|
+
|
187
|
+
### #raise(\*args) → fiber
|
188
|
+
|
189
|
+
Raises an error in the context of the fiber. The given exception can be a string
|
190
|
+
(for raising `RuntimeError` exceptions with a given message), an exception
|
191
|
+
class, or an exception instance. If no argument is given, a `RuntimeError` will
|
192
|
+
be raised. Uncaught exceptions will be propagated.
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
f = spin { sleep 1 }
|
196
|
+
f.raise('foo') # raises a RuntimeError
|
197
|
+
f.raise(MyException, 'my error message') # exception class with message
|
198
|
+
f.raise(MyException.new('my error message')) # exception instance
|
199
|
+
# or simply
|
200
|
+
f.raise
|
201
|
+
```
|
202
|
+
|
203
|
+
### #receive → object
|
204
|
+
|
205
|
+
Pops the first message from the fiber's mailbox. If no message is available,
|
206
|
+
`#receive` will block until a message is pushed to the mailbox. The received
|
207
|
+
message can be any kind of object. This method is complemented by
|
208
|
+
`Fiber#<<`/`Fiber#send`.
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
spin { Fiber.current.parent << 'hello from child' }
|
212
|
+
message = receive #=> 'hello from child'
|
213
|
+
```
|
214
|
+
|
215
|
+
### #receive_pending → [*object]
|
216
|
+
|
217
|
+
Returns all messages currently in the mailbox, emptying the mailbox. This method
|
218
|
+
does not block if no the mailbox is already empty. This method may be used to
|
219
|
+
process any pending messages upon fiber termination:
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
worker = spin do
|
223
|
+
loop do
|
224
|
+
job = receive
|
225
|
+
handle_job(job)
|
226
|
+
end
|
227
|
+
rescue Polyphony::Terminate => e
|
228
|
+
receive_pending.each { |job| handle_job(job) }
|
229
|
+
end
|
230
|
+
```
|
231
|
+
|
232
|
+
### #restart(object = nil) → fiber<br>#reset(object = nil) → fiber
|
233
|
+
|
234
|
+
Restarts the fiber, essentially rerunning the fiber's associated block,
|
235
|
+
restoring it to its primary state. If the fiber is already terminated, a new
|
236
|
+
fiber will be created and returned. If the fiber's block takes an argument, its
|
237
|
+
value can be set by passing it to `#restart`.
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
counter = 0
|
241
|
+
f = spin { sleep 1; counter += 1 }
|
242
|
+
f.await #=> 1
|
243
|
+
f.restart
|
244
|
+
f.await #=> 2
|
245
|
+
```
|
246
|
+
|
247
|
+
### #result → object
|
248
|
+
|
249
|
+
Returns the result of the fiber. If the fiber has not yet terminated, `nil` is
|
250
|
+
returned. If the fiber terminated with an uncaught exception, the exception is
|
251
|
+
returned.
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
f = spin { sleep 1; 'foo' }
|
255
|
+
f.await
|
256
|
+
f.result #=> 'foo'
|
257
|
+
```
|
258
|
+
|
259
|
+
### #running? → true or false
|
260
|
+
|
261
|
+
Returns true if the fiber is not yet terminated.
|
262
|
+
|
263
|
+
### #schedule(object = nil) → fiber
|
264
|
+
|
265
|
+
Adds the fiber to its thread's run queue. The fiber will be eventually resumed
|
266
|
+
with the given value, which can be any object. If an exception is given, it will
|
267
|
+
be raised in the context of the fiber upon resuming. If the fiber is already on
|
268
|
+
the run queue, the resume value will be updated.
|
269
|
+
|
270
|
+
```ruby
|
271
|
+
f = spin do
|
272
|
+
result = suspend
|
273
|
+
p result
|
274
|
+
end
|
275
|
+
|
276
|
+
sleep 0.1
|
277
|
+
f.schedule 'foo'
|
278
|
+
f.await
|
279
|
+
#=> 'foo'
|
280
|
+
```
|
281
|
+
|
282
|
+
### #shutdown_all_children → fiber
|
283
|
+
|
284
|
+
Terminates all of the fiber's child fibers and blocks until all are terminated.
|
285
|
+
This method is can be used to replace calls to `#terminate_all_children`
|
286
|
+
followed by `#await_all_children`.
|
287
|
+
|
288
|
+
```ruby
|
289
|
+
jobs.each { |j| spin { process(j) } }
|
290
|
+
sleep 1
|
291
|
+
shutdown_all_children
|
292
|
+
```
|
293
|
+
|
294
|
+
### #spin(tag = nil, { block }) → fiber
|
295
|
+
|
296
|
+
Creates a new fiber with self as its parent. The returned fiber is put on the
|
297
|
+
run queue of the parent fiber's associated thread. A tag of any object type can
|
298
|
+
be associated with the fiber. Note that `Object#spin` is a shortcut for
|
299
|
+
`Fiber.current.spin`.
|
300
|
+
|
301
|
+
```ruby
|
302
|
+
f = Fiber.current.spin { |x|'foo' }
|
303
|
+
f.await #=> 'foo'
|
304
|
+
```
|
305
|
+
|
306
|
+
If the block takes an argument, its value can be controlled by explicitly
|
307
|
+
calling `Fiber#schedule`. The result of the given block (the value of the last
|
308
|
+
statement in the block) can be retrieved using `Fiber#result` or by otherwise
|
309
|
+
using fiber control APIs such as `Fiber#await`.
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
f = spin { |x| x * 10 }
|
313
|
+
f.schedule(2)
|
314
|
+
f.await #=> 20
|
315
|
+
```
|
316
|
+
|
317
|
+
### #state → symbol
|
318
|
+
|
319
|
+
Returns the fiber's current state, which can be any of the following:
|
320
|
+
|
321
|
+
- `:waiting` - the fiber is currently waiting for an operation to complete.
|
322
|
+
- `:runnable` - the fiber is scheduled to be resumed (put on the run queue).
|
323
|
+
- `:running` - the fiber is currently running.
|
324
|
+
- `:dead` - the fiber has terminated.
|
325
|
+
|
326
|
+
### #supervise(opts = {}) → fiber
|
327
|
+
|
328
|
+
Supervises all child fibers, optionally restarting any fiber that terminates.
|
329
|
+
|
330
|
+
The given `opts` argument controls the behaviour of the supervision. The
|
331
|
+
following options are currently supported:
|
332
|
+
|
333
|
+
- `:restart`: restart options
|
334
|
+
- `nil` - Child fibers are not restarted (default behaviour).
|
335
|
+
- `true` - Any child fiber that terminates is restarted.
|
336
|
+
- `:on_error` - Any child fiber that terminates with an uncaught exception is
|
337
|
+
restarted.
|
338
|
+
- `:watcher`: a fiber watching supervision events.
|
339
|
+
|
340
|
+
If a watcher fiber is specified, it will receive supervision events to its
|
341
|
+
mailbox. The events are of the form `[<event_type>, <fiber>]`, for example
|
342
|
+
`[:restart, child_fiber_1]`. Here's an example of using a watcher fiber:
|
343
|
+
|
344
|
+
```ruby
|
345
|
+
watcher = spin_loop do
|
346
|
+
kind, fiber = receive
|
347
|
+
case kind
|
348
|
+
when :restart
|
349
|
+
puts "fiber #{fiber.inspect} restarted"
|
350
|
+
end
|
351
|
+
end
|
352
|
+
...
|
353
|
+
supervise(restart: true, watcher: watcher)
|
354
|
+
```
|
355
|
+
|
356
|
+
### #tag → object
|
357
|
+
|
358
|
+
Returns the tag associated with the fiber, normally passed to `Fiber#spin`. The
|
359
|
+
tag can be any kind of object. The default tag is `nil`.
|
360
|
+
|
361
|
+
```ruby
|
362
|
+
f = spin(:worker) { do_some_work }
|
363
|
+
f.tag #=> :worker
|
364
|
+
```
|
365
|
+
|
366
|
+
### #tag=(object) → object
|
367
|
+
|
368
|
+
Sets the tag associated with the fiber. The tag can be any kind of object.
|
369
|
+
|
370
|
+
```ruby
|
371
|
+
f = spin { do_some_work }
|
372
|
+
f.tag = :worker
|
373
|
+
```
|
374
|
+
|
375
|
+
### #terminate → fiber
|
376
|
+
|
377
|
+
Terminates the fiber by raising a `Polyphony::Terminate` exception. The
|
378
|
+
exception is not propagated. Note that the fiber is not guaranteed to terminate
|
379
|
+
before `#terminate` returns. The fiber will need to run first in order to raise
|
380
|
+
the `Terminate` exception and terminate. This method is normally coupled with
|
381
|
+
`Fiber#await`:
|
382
|
+
|
383
|
+
```ruby
|
384
|
+
f1 = spin { sleep 1 }
|
385
|
+
f2 = spin { sleep 2 }
|
386
|
+
|
387
|
+
f1.await
|
388
|
+
|
389
|
+
f2.terminate
|
390
|
+
f2.await
|
391
|
+
```
|
392
|
+
|
393
|
+
### #terminate_all_children → fiber
|
394
|
+
|
395
|
+
Terminates all of the fiber's child fibers. Note that `#terminate_all_children`
|
396
|
+
does not acutally wait for all child fibers to terminate. This method is
|
397
|
+
normally coupled with `Fiber#await_all_children`. See also `Fiber#shutdown_all_children`.
|
398
|
+
|
399
|
+
```ruby
|
400
|
+
jobs.each { |j| spin { process(j) } }
|
401
|
+
sleep 1
|
402
|
+
terminate_all_children
|
403
|
+
await_all_children
|
404
|
+
```
|
405
|
+
|
406
|
+
### #thread → thread
|
407
|
+
|
408
|
+
Returns the thread to which the fiber belongs.
|
409
|
+
|
410
|
+
```ruby
|
411
|
+
f = spin(:worker) { do_some_work }
|
412
|
+
f.thread #=> Thread.current
|
413
|
+
```
|
414
|
+
|
415
|
+
### #when_done({ block }) → fiber
|
416
|
+
|
417
|
+
Installs a hook to be called when the fiber is terminated. The block will be
|
418
|
+
called with the fiber's result. If the fiber terminates with an uncaught
|
419
|
+
exception, the exception will be passed to the block.
|
420
|
+
|
421
|
+
```ruby
|
422
|
+
f = spin { 'foo' }
|
423
|
+
f.when_done { |r| puts "got #{r} from fiber" }
|
424
|
+
f.await #=> STDOUT: 'got foo from fiber'
|
425
|
+
```
|