ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.415 by root, Wed Sep 25 11:16:25 2013 UTC vs.
Revision 1.416 by root, Tue Dec 17 16:43:15 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines