… | |
… | |
487 | |
487 | |
488 | Example: fork a process and wait for it |
488 | Example: fork a process and wait for it |
489 | |
489 | |
490 | my $done = AnyEvent->condvar; |
490 | my $done = AnyEvent->condvar; |
491 | |
491 | |
|
|
492 | # this forks and immediately calls exit in the child. this |
|
|
493 | # normally has all sorts of bad consequences for your parent, |
|
|
494 | # so take this as an example only. always fork and exec, |
|
|
495 | # or call POSIX::_exit, in real code. |
492 | my $pid = fork or exit 5; |
496 | my $pid = fork or exit 5; |
493 | |
497 | |
494 | my $w = AnyEvent->child ( |
498 | my $w = AnyEvent->child ( |
495 | pid => $pid, |
499 | pid => $pid, |
496 | cb => sub { |
500 | cb => sub { |
… | |
… | |
1181 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
1185 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
1182 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
1186 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
1183 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
1187 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
1184 | file I/O, and much more. |
1188 | file I/O, and much more. |
1185 | |
1189 | |
|
|
1190 | =item L<AnyEvent::Fork>, L<AnyEvent::Fork::RPC>, L<AnyEvent::Fork::Pool>, L<AnyEvent::Fork::Remote> |
|
|
1191 | |
|
|
1192 | These let you safely fork new subprocesses, either locally or |
|
|
1193 | remotely (e.g.v ia ssh), using some RPC protocol or not, without |
|
|
1194 | the limitations normally imposed by fork (AnyEvent works fine for |
|
|
1195 | example). Dynamically-resized worker pools are obviously included as well. |
|
|
1196 | |
|
|
1197 | And they are quite tiny and fast as well - "abusing" L<AnyEvent::Fork> |
|
|
1198 | just to exec external programs can easily beat using C<fork> and C<exec> |
|
|
1199 | (or even C<system>) in most programs. |
|
|
1200 | |
1186 | =item L<AnyEvent::Filesys::Notify> |
1201 | =item L<AnyEvent::Filesys::Notify> |
1187 | |
1202 | |
1188 | AnyEvent is good for non-blocking stuff, but it can't detect file or |
1203 | AnyEvent is good for non-blocking stuff, but it can't detect file or |
1189 | path changes (e.g. "watch this directory for new files", "watch this |
1204 | path changes (e.g. "watch this directory for new files", "watch this |
1190 | file for changes"). The L<AnyEvent::Filesys::Notify> module promises to |
1205 | file for changes"). The L<AnyEvent::Filesys::Notify> module promises to |
1191 | do just that in a portbale fashion, supporting inotify on GNU/Linux and |
1206 | do just that in a portbale fashion, supporting inotify on GNU/Linux and |
1192 | some weird, without doubt broken, stuff on OS X to monitor files. It can |
1207 | some weird, without doubt broken, stuff on OS X to monitor files. It can |
1193 | fall back to blocking scans at regular intervals transparently on other |
1208 | fall back to blocking scans at regular intervals transparently on other |
1194 | platforms, so it's about as portable as it gets. |
1209 | platforms, so it's about as portable as it gets. |
1195 | |
1210 | |
1196 | (I haven't used it myself, but I haven't heard anybody complaining about |
1211 | (I haven't used it myself, but it seems the biggest problem with it is |
1197 | it yet). |
1212 | it quite bad performance). |
1198 | |
1213 | |
1199 | =item L<AnyEvent::DBI> |
1214 | =item L<AnyEvent::DBI> |
1200 | |
1215 | |
1201 | Executes L<DBI> requests asynchronously in a proxy process for you, |
1216 | Executes L<DBI> requests asynchronously in a proxy process for you, |
1202 | notifying you in an event-based way when the operation is finished. |
1217 | notifying you in an event-based way when the operation is finished. |
1203 | |
|
|
1204 | =item L<AnyEvent::HTTPD> |
|
|
1205 | |
|
|
1206 | A simple embedded webserver. |
|
|
1207 | |
1218 | |
1208 | =item L<AnyEvent::FastPing> |
1219 | =item L<AnyEvent::FastPing> |
1209 | |
1220 | |
1210 | The fastest ping in the west. |
1221 | The fastest ping in the west. |
1211 | |
1222 | |
… | |
… | |
2951 | usually happens when the first AnyEvent watcher is created, or the library |
2962 | usually happens when the first AnyEvent watcher is created, or the library |
2952 | is loaded). |
2963 | is loaded). |
2953 | |
2964 | |
2954 | If you have to fork, you must either do so I<before> creating your first |
2965 | If you have to fork, you must either do so I<before> creating your first |
2955 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2966 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2956 | something completely out of the scope of AnyEvent. |
2967 | something completely out of the scope of AnyEvent (see below). |
2957 | |
2968 | |
2958 | The problem of doing event processing in the parent I<and> the child |
2969 | The problem of doing event processing in the parent I<and> the child |
2959 | is much more complicated: even for backends that I<are> fork-aware or |
2970 | is much more complicated: even for backends that I<are> fork-aware or |
2960 | fork-safe, their behaviour is not usually what you want: fork clones all |
2971 | fork-safe, their behaviour is not usually what you want: fork clones all |
2961 | watchers, that means all timers, I/O watchers etc. are active in both |
2972 | watchers, that means all timers, I/O watchers etc. are active in both |
2962 | parent and child, which is almost never what you want. USing C<exec> |
2973 | parent and child, which is almost never what you want. Using C<exec> |
2963 | to start worker children from some kind of manage rprocess is usually |
2974 | to start worker children from some kind of manage prrocess is usually |
2964 | preferred, because it is much easier and cleaner, at the expense of having |
2975 | preferred, because it is much easier and cleaner, at the expense of having |
2965 | to have another binary. |
2976 | to have another binary. |
|
|
2977 | |
|
|
2978 | In addition to logical problems with fork, there are also implementation |
|
|
2979 | problems. For example, on POSIX systems, you cannot fork at all in Perl |
|
|
2980 | code if a thread (I am talking of pthreads here) was ever created in the |
|
|
2981 | process, and this is just the tip of the iceberg. In general, using fork |
|
|
2982 | from Perl is difficult, and attempting to use fork without an exec to |
|
|
2983 | implement some kind of parallel processing is almost certainly doomed. |
|
|
2984 | |
|
|
2985 | To safely fork and exec, you should use a module such as |
|
|
2986 | L<Proc::FastSpawn> that let's you safely fork and exec new processes. |
|
|
2987 | |
|
|
2988 | If you want to do multiprocessing using processes, you can |
|
|
2989 | look at the L<AnyEvent::Fork> module (and some related modules |
|
|
2990 | such as L<AnyEvent::Fork::RPC>, L<AnyEvent::Fork::Pool> and |
|
|
2991 | L<AnyEvent::Fork::Remote>). This module allows you to safely create |
|
|
2992 | subprocesses without any limitations - you can use X11 toolkits or |
|
|
2993 | AnyEvent in the children created by L<AnyEvent::Fork> safely and without |
|
|
2994 | any special precautions. |
2966 | |
2995 | |
2967 | |
2996 | |
2968 | =head1 SECURITY CONSIDERATIONS |
2997 | =head1 SECURITY CONSIDERATIONS |
2969 | |
2998 | |
2970 | AnyEvent can be forced to load any event model via |
2999 | AnyEvent can be forced to load any event model via |