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

Comparing AnyEvent-Fork/Fork.pm (file contents):
Revision 1.20 by root, Sat Apr 6 03:35:36 2013 UTC vs.
Revision 1.23 by root, Sat Apr 6 08:29:43 2013 UTC

61 while (my $socket = $listener->accept) { 61 while (my $socket = $listener->accept) {
62 # do sth. with new socket 62 # do sth. with new socket
63 } 63 }
64 } 64 }
65 65
66 ##################################################################
67 # use AnyEvent::Fork as a faster fork+exec
68
69 # this runs /bin/echo hi, with stdout redirected to /tmp/log
70 # and stderr to the communications socket. it is usually faster
71 # than fork+exec, but still let's you prepare the environment.
72
73 open my $output, ">/tmp/log" or die "$!";
74
75 AnyEvent::Fork
76 ->new
77 ->eval ('
78 sub run {
79 my ($fh, $output, @cmd) = @_;
80
81 # perl will clear close-on-exec on STDOUT/STDERR
82 open STDOUT, ">&", $output or die;
83 open STDERR, ">&", $fh or die;
84
85 exec @cmd;
86 }
87 ')
88 ->send_fh ($output)
89 ->send_arg ("/bin/echo", "hi")
90 ->run ("run", my $cv = AE::cv);
91
92 my $stderr = $cv->recv;
93
66=head1 DESCRIPTION 94=head1 DESCRIPTION
67 95
68This module allows you to create new processes, without actually forking 96This module allows you to create new processes, without actually forking
69them from your current process (avoiding the problems of forking), but 97them from your current process (avoiding the problems of forking), but
70preserving most of the advantages of fork. 98preserving most of the advantages of fork.
258use AnyEvent; 286use AnyEvent;
259use AnyEvent::Util (); 287use AnyEvent::Util ();
260 288
261use IO::FDPass; 289use IO::FDPass;
262 290
263our $VERSION = 0.2; 291our $VERSION = 0.5;
264 292
265our $PERL; # the path to the perl interpreter, deduces with various forms of magic 293our $PERL; # the path to the perl interpreter, deduces with various forms of magic
266 294
267=item my $pool = new AnyEvent::Fork key => value... 295=item my $pool = new AnyEvent::Fork key => value...
268 296
493} 521}
494 522
495=item $proc = $proc->eval ($perlcode, @args) 523=item $proc = $proc->eval ($perlcode, @args)
496 524
497Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to 525Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to
498the strings specified by C<@args>. 526the strings specified by C<@args>, in the "main" package.
499 527
500This call is meant to do any custom initialisation that might be required 528This call is meant to do any custom initialisation that might be required
501(for example, the C<require> method uses it). It's not supposed to be used 529(for example, the C<require> method uses it). It's not supposed to be used
502to completely take over the process, use C<run> for that. 530to completely take over the process, use C<run> for that.
503 531
504The code will usually be executed after this call returns, and there is no 532The code will usually be executed after this call returns, and there is no
505way to pass anything back to the calling process. Any evaluation errors 533way to pass anything back to the calling process. Any evaluation errors
506will be reported to stderr and cause the process to exit. 534will be reported to stderr and cause the process to exit.
535
536If you want to execute some code to take over the process (see the
537"fork+exec" example in the SYNOPSIS), you should compile a function via
538C<eval> first, and then call it via C<run>. This also gives you access to
539any arguments passed via the C<send_xxx> methods, such as file handles.
507 540
508Returns the process object for easy chaining of method calls. 541Returns the process object for easy chaining of method calls.
509 542
510=cut 543=cut
511 544
587 $self 620 $self
588} 621}
589 622
590=item $proc->run ($func, $cb->($fh)) 623=item $proc->run ($func, $cb->($fh))
591 624
592Enter the function specified by the fully qualified name in C<$func> in 625Enter the function specified by the function name in C<$func> in the
593the process. The function is called with the communication socket as first 626process. The function is called with the communication socket as first
594argument, followed by all file handles and string arguments sent earlier 627argument, followed by all file handles and string arguments sent earlier
595via C<send_fh> and C<send_arg> methods, in the order they were called. 628via C<send_fh> and C<send_arg> methods, in the order they were called.
596 629
597If the called function returns, the process exits. 630The function name should be fully qualified, but if it isn't, it will be
631looked up in the main package.
598 632
599Preparing the process can take time - when the process is ready, the 633If the called function returns, doesn't exist, or any error occurs, the
634process exits.
635
636Preparing the process is done in the background - when all commands have
600callback is invoked with the local communications socket as argument. 637been sent, the callback is invoked with the local communications socket
638as argument. At this point you can start using the socket in any way you
639like.
601 640
602The process object becomes unusable on return from this function. 641The process object becomes unusable on return from this function - any
642further method calls result in undefined behaviour.
603 643
604If the communication socket isn't used, it should be closed on both sides, 644If the communication socket isn't used, it should be closed on both sides,
605to save on kernel memory. 645to save on kernel memory.
606 646
607The socket is non-blocking in the parent, and blocking in the newly 647The socket is non-blocking in the parent, and blocking in the newly
608created process. The close-on-exec flag is set on both. Even if not used 648created process. The close-on-exec flag is set in both.
649
609otherwise, the socket can be a good indicator for the existence of the 650Even if not used otherwise, the socket can be a good indicator for the
610process - if the other process exits, you get a readable event on it, 651existence of the process - if the other process exits, you get a readable
611because exiting the process closes the socket (if it didn't create any 652event on it, because exiting the process closes the socket (if it didn't
612children using fork). 653create any children using fork).
613 654
614Example: create a template for a process pool, pass a few strings, some 655Example: create a template for a process pool, pass a few strings, some
615file handles, then fork, pass one more string, and run some code. 656file handles, then fork, pass one more string, and run some code.
616 657
617 my $pool = AnyEvent::Fork 658 my $pool = AnyEvent::Fork
625 ->send_arg ("str3") 666 ->send_arg ("str3")
626 ->run ("Some::function", sub { 667 ->run ("Some::function", sub {
627 my ($fh) = @_; 668 my ($fh) = @_;
628 669
629 # fh is nonblocking, but we trust that the OS can accept these 670 # fh is nonblocking, but we trust that the OS can accept these
630 # extra 3 octets anyway. 671 # few octets anyway.
631 syswrite $fh, "hi #$_\n"; 672 syswrite $fh, "hi #$_\n";
632 673
633 # $fh is being closed here, as we don't store it anywhere 674 # $fh is being closed here, as we don't store it anywhere
634 }); 675 });
635 } 676 }
637 # Some::function might look like this - all parameters passed before fork 678 # Some::function might look like this - all parameters passed before fork
638 # and after will be passed, in order, after the communications socket. 679 # and after will be passed, in order, after the communications socket.
639 sub Some::function { 680 sub Some::function {
640 my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; 681 my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_;
641 682
642 print scalar <$fh>; # prints "hi 1\n" and "hi 2\n" 683 print scalar <$fh>; # prints "hi #1\n" and "hi #2\n" in any order
643 } 684 }
644 685
645=cut 686=cut
646 687
647sub run { 688sub run {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines