… | |
… | |
2534 | |
2534 | |
2535 | |
2535 | |
2536 | =head1 FORK |
2536 | =head1 FORK |
2537 | |
2537 | |
2538 | Most event libraries are not fork-safe. The ones who are usually are |
2538 | Most event libraries are not fork-safe. The ones who are usually are |
2539 | because they rely on inefficient but fork-safe C<select> or C<poll> |
2539 | because they rely on inefficient but fork-safe C<select> or C<poll> calls |
2540 | calls. Only L<EV> is fully fork-aware. |
2540 | - higher performance APIs such as BSD's kqueue or the dreaded Linux epoll |
|
|
2541 | are usually badly thought-out hacks that are incompatible with fork in |
|
|
2542 | one way or another. Only L<EV> is fully fork-aware and ensures that you |
|
|
2543 | continue event-processing in both parent and child (or both, if you know |
|
|
2544 | what you are doing). |
2541 | |
2545 | |
2542 | This means that, in general, you cannot fork and do event processing |
2546 | This means that, in general, you cannot fork and do event processing in |
2543 | in the child if a watcher was created before the fork (which in turn |
2547 | the child if the event library was initialised before the fork (which |
2544 | initialises the event library). |
2548 | usually happens when the first AnyEvent watcher is created, or the library |
|
|
2549 | is loaded). |
2545 | |
2550 | |
2546 | If you have to fork, you must either do so I<before> creating your first |
2551 | If you have to fork, you must either do so I<before> creating your first |
2547 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2552 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2548 | something completely out of the scope of AnyEvent. |
2553 | something completely out of the scope of AnyEvent. |
2549 | |
2554 | |
2550 | The problem of doing event processing in the parent I<and> the child |
2555 | The problem of doing event processing in the parent I<and> the child |
2551 | is much more complicated: even for backends that I<are> fork-aware or |
2556 | is much more complicated: even for backends that I<are> fork-aware or |
2552 | fork-safe, their behaviour is not usually what you want: fork clones all |
2557 | fork-safe, their behaviour is not usually what you want: fork clones all |
2553 | watchers, that means all timers, I/O watchers etc. are active in both |
2558 | watchers, that means all timers, I/O watchers etc. are active in both |
2554 | parent and child, which is almost never what you want. |
2559 | parent and child, which is almost never what you want. USing C<exec> |
|
|
2560 | to start worker children from some kind of manage rprocess is usually |
|
|
2561 | preferred, because it is much easier and cleaner, at the expense of having |
|
|
2562 | to have another binary. |
2555 | |
2563 | |
2556 | |
2564 | |
2557 | =head1 SECURITY CONSIDERATIONS |
2565 | =head1 SECURITY CONSIDERATIONS |
2558 | |
2566 | |
2559 | AnyEvent can be forced to load any event model via |
2567 | AnyEvent can be forced to load any event model via |