… | |
… | |
1130 | But from within a coro, you often just want to write this: |
1130 | But from within a coro, you often just want to write this: |
1131 | |
1131 | |
1132 | my $status = wait_for_child $pid; |
1132 | my $status = wait_for_child $pid; |
1133 | |
1133 | |
1134 | Coro offers two functions specifically designed to make this easy, |
1134 | Coro offers two functions specifically designed to make this easy, |
1135 | C<Coro::rouse_cb> and C<Coro::rouse_wait>. |
1135 | C<rouse_cb> and C<rouse_wait>. |
1136 | |
1136 | |
1137 | The first function, C<rouse_cb>, generates and returns a callback that, |
1137 | The first function, C<rouse_cb>, generates and returns a callback that, |
1138 | when invoked, will save its arguments and notify the coro that |
1138 | when invoked, will save its arguments and notify the coro that |
1139 | created the callback. |
1139 | created the callback. |
1140 | |
1140 | |
… | |
… | |
1146 | function mentioned above: |
1146 | function mentioned above: |
1147 | |
1147 | |
1148 | sub wait_for_child($) { |
1148 | sub wait_for_child($) { |
1149 | my ($pid) = @_; |
1149 | my ($pid) = @_; |
1150 | |
1150 | |
1151 | my $watcher = AnyEvent->child (pid => $pid, cb => Coro::rouse_cb); |
1151 | my $watcher = AnyEvent->child (pid => $pid, cb => rouse_cb); |
1152 | |
1152 | |
1153 | my ($rpid, $rstatus) = Coro::rouse_wait; |
1153 | my ($rpid, $rstatus) = rouse_wait; |
1154 | $rstatus |
1154 | $rstatus |
1155 | } |
1155 | } |
1156 | |
1156 | |
1157 | In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough, |
1157 | In the case where C<rouse_cb> and C<rouse_wait> are not flexible enough, |
1158 | you can roll your own, using C<schedule> and C<ready>: |
1158 | you can roll your own, using C<schedule> and C<ready>: |