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.18 by root, Sat Apr 6 01:33:56 2013 UTC vs.
Revision 1.20 by root, Sat Apr 6 03:35:36 2013 UTC

284 my $self = shift; 284 my $self = shift;
285 285
286 # ideally, we would want to use "a (w/a)*" as format string, but perl 286 # ideally, we would want to use "a (w/a)*" as format string, but perl
287 # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack 287 # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack
288 # it. 288 # it.
289 push @{ $self->[2] }, pack "L/a*", pack "(w/a*)*", @_; 289 push @{ $self->[2] }, pack "a L/a*", $_[0], $_[1];
290 290
291 unless ($self->[3]) { 291 $self->[3] ||= AE::io $self->[1], 1, sub {
292 my $wcb = sub {
293 do { 292 do {
294 # send the next "thing" in the queue - either a reference to an fh, 293 # send the next "thing" in the queue - either a reference to an fh,
295 # or a plain string. 294 # or a plain string.
296 295
297 if (ref $self->[2][0]) { 296 if (ref $self->[2][0]) {
298 # send fh 297 # send fh
299 unless (IO::FDPass::send fileno $self->[1], fileno ${ $self->[2][0] }) { 298 unless (IO::FDPass::send fileno $self->[1], fileno ${ $self->[2][0] }) {
300 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; 299 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK;
301 undef $self->[3]; 300 undef $self->[3];
302 die "AnyEvent::Fork: file descriptor send failure: $!"; 301 die "AnyEvent::Fork: file descriptor send failure: $!";
303 }
304
305 shift @{ $self->[2] };
306
307 } else {
308 # send string
309 my $len = syswrite $self->[1], $self->[2][0];
310
311 unless ($len) {
312 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK;
313 undef $self->[3];
314 die "AnyEvent::Fork: command write failure: $!";
315 }
316
317 substr $self->[2][0], 0, $len, "";
318 shift @{ $self->[2] } unless length $self->[2][0];
319 } 302 }
303
304 shift @{ $self->[2] };
305
306 } else {
307 # send string
308 my $len = syswrite $self->[1], $self->[2][0];
309
310 unless ($len) {
311 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK;
312 undef $self->[3];
313 die "AnyEvent::Fork: command write failure: $!";
314 }
315
316 substr $self->[2][0], 0, $len, "";
317 shift @{ $self->[2] } unless length $self->[2][0];
318 }
320 } while @{ $self->[2] }; 319 } while @{ $self->[2] };
321 320
322 # everything written 321 # everything written
323 undef $self->[3]; 322 undef $self->[3];
323
324 # invoke run callback 324 # invoke run callback, if any
325 $self->[0]->($self->[1]) if $self->[0]; 325 $self->[4]->($self->[1]) if $self->[4];
326 };
327
328 $wcb->();
329
330 $self->[3] ||= AE::io $self->[1], 1, $wcb
331 if @{ $self->[2] };
332 } 326 };
333 327
334 () # make sure we don't leak the watcher 328 () # make sure we don't leak the watcher
335} 329}
336 330
337sub _new { 331sub _new {
338 my ($self, $fh) = @_; 332 my ($self, $fh, $pid) = @_;
339 333
340 AnyEvent::Util::fh_nonblocking $fh, 1; 334 AnyEvent::Util::fh_nonblocking $fh, 1;
341 335
342 $self = bless [ 336 $self = bless [
343 undef, # run callback 337 $pid,
344 $fh, 338 $fh,
345 [], # write queue - strings or fd's 339 [], # write queue - strings or fd's
346 undef, # AE watcher 340 undef, # AE watcher
347 ], $self; 341 ], $self;
348 342
366 exit 0; 360 exit 0;
367 } elsif (!$pid) { 361 } elsif (!$pid) {
368 die "AnyEvent::Fork::Early/Template: unable to fork template process: $!"; 362 die "AnyEvent::Fork::Early/Template: unable to fork template process: $!";
369 } 363 }
370 364
371 AnyEvent::Fork->_new ($fh) 365 AnyEvent::Fork->_new ($fh, $pid)
372} 366}
373 367
374=item my $proc = new AnyEvent::Fork 368=item my $proc = new AnyEvent::Fork
375 369
376Create a new "empty" perl interpreter process and returns its process 370Create a new "empty" perl interpreter process and returns its process
469 # quick. also doesn't work in win32. of course. what did you expect 463 # quick. also doesn't work in win32. of course. what did you expect
470 #local $ENV{PERL5LIB} = join ":", grep !ref, @INC; 464 #local $ENV{PERL5LIB} = join ":", grep !ref, @INC;
471 my %env = %ENV; 465 my %env = %ENV;
472 $env{PERL5LIB} = join +($^O eq "MSWin32" ? ";" : ":"), grep !ref, @INC; 466 $env{PERL5LIB} = join +($^O eq "MSWin32" ? ";" : ":"), grep !ref, @INC;
473 467
474 Proc::FastSpawn::spawn ( 468 my $pid = Proc::FastSpawn::spawn (
475 $perl, 469 $perl,
476 ["perl", "-MAnyEvent::Fork::Serve", "-e", "AnyEvent::Fork::Serve::me", fileno $slave, $$], 470 ["perl", "-MAnyEvent::Fork::Serve", "-e", "AnyEvent::Fork::Serve::me", fileno $slave, $$],
477 [map "$_=$env{$_}", keys %env], 471 [map "$_=$env{$_}", keys %env],
478 ) or die "unable to spawn AnyEvent::Fork server: $!"; 472 ) or die "unable to spawn AnyEvent::Fork server: $!";
479 473
480 $self->_new ($fh) 474 $self->_new ($fh, $pid)
475}
476
477=item $pid = $proc->pid
478
479Returns the process id of the process I<iff it is a direct child of the
480process> running AnyEvent::Fork, and C<undef> otherwise.
481
482Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and
483L<AnyEvent::Fork::Template> are direct children, and you are responsible
484to clean up their zombies when they die.
485
486All other processes are not direct children, and will be cleaned up by
487AnyEvent::Fork.
488
489=cut
490
491sub pid {
492 $_[0][0]
481} 493}
482 494
483=item $proc = $proc->eval ($perlcode, @args) 495=item $proc = $proc->eval ($perlcode, @args)
484 496
485Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to 497Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to
498=cut 510=cut
499 511
500sub eval { 512sub eval {
501 my ($self, $code, @args) = @_; 513 my ($self, $code, @args) = @_;
502 514
503 $self->_cmd (e => $code, @args); 515 $self->_cmd (e => pack "(w/a*)*", $code, @args);
504 516
505 $self 517 $self
506} 518}
507 519
508=item $proc = $proc->require ($module, ...) 520=item $proc = $proc->require ($module, ...)
568=cut 580=cut
569 581
570sub send_arg { 582sub send_arg {
571 my ($self, @arg) = @_; 583 my ($self, @arg) = @_;
572 584
573 $self->_cmd (a => @arg); 585 $self->_cmd (a => pack "(w/a*)*", @arg);
574 586
575 $self 587 $self
576} 588}
577 589
578=item $proc->run ($func, $cb->($fh)) 590=item $proc->run ($func, $cb->($fh))
633=cut 645=cut
634 646
635sub run { 647sub run {
636 my ($self, $func, $cb) = @_; 648 my ($self, $func, $cb) = @_;
637 649
638 $self->[0] = $cb; 650 $self->[4] = $cb;
639 $self->_cmd (r => $func); 651 $self->_cmd (r => $func);
640} 652}
641 653
642=back 654=back
643 655
690This section lists typical problems that remain. I hope by recognising 702This section lists typical problems that remain. I hope by recognising
691them, most can be avoided. 703them, most can be avoided.
692 704
693=over 4 705=over 4
694 706
695=item exit runs destructors
696
697=item "leaked" file descriptors for exec'ed processes 707=item "leaked" file descriptors for exec'ed processes
698 708
699POSIX systems inherit file descriptors by default when exec'ing a new 709POSIX systems inherit file descriptors by default when exec'ing a new
700process. While perl itself laudably sets the close-on-exec flags on new 710process. While perl itself laudably sets the close-on-exec flags on new
701file handles, most C libraries don't care, and even if all cared, it's 711file handles, most C libraries don't care, and even if all cared, it's
741 751
742The solution is to either not load these modules before use'ing 752The solution is to either not load these modules before use'ing
743L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay 753L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay
744initialising them, for example, by calling C<init Gtk2> manually. 754initialising them, for example, by calling C<init Gtk2> manually.
745 755
756=item exit runs destructors
757
758This only applies to users of Lc<AnyEvent::Fork:Early> and
759L<AnyEvent::Fork::Template>.
760
761When a process created by AnyEvent::Fork exits, it might do so by calling
762exit, or simply letting perl reach the end of the program. At which point
763Perl runs all destructors.
764
765Not all destructors are fork-safe - for example, an object that represents
766the connection to an X display might tell the X server to free resources,
767which is inconvenient when the "real" object in the parent still needs to
768use them.
769
770This is obviously not a problem for L<AnyEvent::Fork::Early>, as you used
771it as the very first thing, right?
772
773It is a problem for L<AnyEvent::Fork::Template> though - and the solution
774is to not create objects with nontrivial destructors that might have an
775effect outside of Perl.
776
746=back 777=back
747 778
748=head1 PORTABILITY NOTES 779=head1 PORTABILITY NOTES
749 780
750Native win32 perls are somewhat supported (AnyEvent::Fork::Early is a nop, 781Native win32 perls are somewhat supported (AnyEvent::Fork::Early is a nop,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines