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.41 by root, Mon Apr 8 03:20:53 2013 UTC vs.
Revision 1.43 by root, Thu Apr 18 10:47:48 2013 UTC

34This module only creates processes and lets you pass file handles and 34This module only creates processes and lets you pass file handles and
35strings to it, and run perl code. It does not implement any kind of RPC - 35strings to it, and run perl code. It does not implement any kind of RPC -
36there is no back channel from the process back to you, and there is no RPC 36there is no back channel from the process back to you, and there is no RPC
37or message passing going on. 37or message passing going on.
38 38
39If you need some form of RPC, you can either implement it yourself 39If you need some form of RPC, you could use the L<AnyEvent::Fork::RPC>
40in whatever way you like, use some message-passing module such 40companion module, which adds simple RPC/job queueing to a process created
41as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use 41by this module.
42L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, 42
43and so on. 43Or you can implement it yourself in whatever way you like, use some
44message-passing module such as L<AnyEvent::MP>, some pipe such as
45L<AnyEvent::ZeroMQ>, use L<AnyEvent::Handle> on both sides to send
46e.g. JSON or Storable messages, and so on.
44 47
45=head2 COMPARISON TO OTHER MODULES 48=head2 COMPARISON TO OTHER MODULES
46 49
47There is an abundance of modules on CPAN that do "something fork", such as 50There is an abundance of modules on CPAN that do "something fork", such as
48L<Parallel::ForkManager>, L<AnyEvent::ForkManager>, L<AnyEvent::Worker> 51L<Parallel::ForkManager>, L<AnyEvent::ForkManager>, L<AnyEvent::Worker>
372 375
373use IO::FDPass; 376use IO::FDPass;
374 377
375our $VERSION = 0.6; 378our $VERSION = 0.6;
376 379
377=over 4
378
379=back
380
381=cut
382
383# the early fork template process 380# the early fork template process
384our $EARLY; 381our $EARLY;
385 382
386# the empty template process 383# the empty template process
387our $TEMPLATE; 384our $TEMPLATE;
385
386sub QUEUE() { 0 }
387sub FH() { 1 }
388sub WW() { 2 }
389sub PID() { 3 }
390sub CB() { 4 }
391
392sub _new {
393 my ($self, $fh, $pid) = @_;
394
395 AnyEvent::Util::fh_nonblocking $fh, 1;
396
397 $self = bless [
398 [], # write queue - strings or fd's
399 $fh,
400 undef, # AE watcher
401 $pid,
402 ], $self;
403
404 $self
405}
388 406
389sub _cmd { 407sub _cmd {
390 my $self = shift; 408 my $self = shift;
391 409
392 # ideally, we would want to use "a (w/a)*" as format string, but perl 410 # ideally, we would want to use "a (w/a)*" as format string, but perl
393 # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack 411 # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack
394 # it. 412 # it.
395 push @{ $self->[2] }, pack "a L/a*", $_[0], $_[1]; 413 push @{ $self->[QUEUE] }, pack "a L/a*", $_[0], $_[1];
396 414
397 $self->[3] ||= AE::io $self->[1], 1, sub { 415 $self->[WW] ||= AE::io $self->[FH], 1, sub {
398 do { 416 do {
399 # send the next "thing" in the queue - either a reference to an fh, 417 # send the next "thing" in the queue - either a reference to an fh,
400 # or a plain string. 418 # or a plain string.
401 419
402 if (ref $self->[2][0]) { 420 if (ref $self->[QUEUE][0]) {
403 # send fh 421 # send fh
404 unless (IO::FDPass::send fileno $self->[1], fileno ${ $self->[2][0] }) { 422 unless (IO::FDPass::send fileno $self->[FH], fileno ${ $self->[QUEUE][0] }) {
405 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; 423 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK;
406 undef $self->[3]; 424 undef $self->[WW];
407 die "AnyEvent::Fork: file descriptor send failure: $!"; 425 die "AnyEvent::Fork: file descriptor send failure: $!";
408 } 426 }
409 427
410 shift @{ $self->[2] }; 428 shift @{ $self->[QUEUE] };
411 429
412 } else { 430 } else {
413 # send string 431 # send string
414 my $len = syswrite $self->[1], $self->[2][0]; 432 my $len = syswrite $self->[FH], $self->[QUEUE][0];
415 433
416 unless ($len) { 434 unless ($len) {
417 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; 435 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK;
418 undef $self->[3]; 436 undef $self->[3];
419 die "AnyEvent::Fork: command write failure: $!"; 437 die "AnyEvent::Fork: command write failure: $!";
420 } 438 }
421 439
422 substr $self->[2][0], 0, $len, ""; 440 substr $self->[QUEUE][0], 0, $len, "";
423 shift @{ $self->[2] } unless length $self->[2][0]; 441 shift @{ $self->[QUEUE] } unless length $self->[QUEUE][0];
424 } 442 }
425 } while @{ $self->[2] }; 443 } while @{ $self->[QUEUE] };
426 444
427 # everything written 445 # everything written
428 undef $self->[3]; 446 undef $self->[WW];
429 447
430 # invoke run callback, if any 448 # invoke run callback, if any
431 $self->[4]->($self->[1]) if $self->[4]; 449 $self->[CB]->($self->[FH]) if $self->[CB];
432 }; 450 };
433 451
434 () # make sure we don't leak the watcher 452 () # make sure we don't leak the watcher
435}
436
437sub _new {
438 my ($self, $fh, $pid) = @_;
439
440 AnyEvent::Util::fh_nonblocking $fh, 1;
441
442 $self = bless [
443 $pid,
444 $fh,
445 [], # write queue - strings or fd's
446 undef, # AE watcher
447 ], $self;
448
449 $self
450} 453}
451 454
452# fork template from current process, used by AnyEvent::Fork::Early/Template 455# fork template from current process, used by AnyEvent::Fork::Early/Template
453sub _new_fork { 456sub _new_fork {
454 my ($fh, $slave) = AnyEvent::Util::portable_socketpair; 457 my ($fh, $slave) = AnyEvent::Util::portable_socketpair;
587AnyEvent::Fork itself. 590AnyEvent::Fork itself.
588 591
589=cut 592=cut
590 593
591sub pid { 594sub pid {
592 $_[0][0] 595 $_[0][PID]
593} 596}
594 597
595=item $proc = $proc->eval ($perlcode, @args) 598=item $proc = $proc->eval ($perlcode, @args)
596 599
597Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to 600Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to
664sub send_fh { 667sub send_fh {
665 my ($self, @fh) = @_; 668 my ($self, @fh) = @_;
666 669
667 for my $fh (@fh) { 670 for my $fh (@fh) {
668 $self->_cmd ("h"); 671 $self->_cmd ("h");
669 push @{ $self->[2] }, \$fh; 672 push @{ $self->[QUEUE] }, \$fh;
670 } 673 }
671 674
672 $self 675 $self
673} 676}
674 677
760=cut 763=cut
761 764
762sub run { 765sub run {
763 my ($self, $func, $cb) = @_; 766 my ($self, $func, $cb) = @_;
764 767
765 $self->[4] = $cb; 768 $self->[CB] = $cb;
766 $self->_cmd (r => $func); 769 $self->_cmd (r => $func);
767} 770}
768 771
769=back 772=back
770 773
798So how can C<< AnyEvent->new >> be faster than a standard fork, even 801So how can C<< AnyEvent->new >> be faster than a standard fork, even
799though it uses the same operations, but adds a lot of overhead? 802though it uses the same operations, but adds a lot of overhead?
800 803
801The difference is simply the process size: forking the 5MB process takes 804The difference is simply the process size: forking the 5MB process takes
802so much longer than forking the 2.5MB template process that the extra 805so much longer than forking the 2.5MB template process that the extra
803overhead introduced is canceled out. 806overhead is canceled out.
804 807
805If the benchmark process grows, the normal fork becomes even slower: 808If the benchmark process grows, the normal fork becomes even slower:
806 809
807 1340 new processes, manual fork of a 20MB process 810 1340 new processes, manual fork of a 20MB process
808 731 new processes, manual fork of a 200MB process 811 731 new processes, manual fork of a 200MB process
907 910
908L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), 911L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter),
909L<AnyEvent::Fork::Template> (to create a process by forking the main 912L<AnyEvent::Fork::Template> (to create a process by forking the main
910program at a convenient time). 913program at a convenient time).
911 914
912=head1 AUTHOR 915=head1 AUTHOR AND CONTACT INFORMATION
913 916
914 Marc Lehmann <schmorp@schmorp.de> 917 Marc Lehmann <schmorp@schmorp.de>
915 http://home.schmorp.de/ 918 http://software.schmorp.de/pkg/AnyEvent-Fork
916 919
917=cut 920=cut
918 921
9191 9221
920 923

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines