… | |
… | |
304 | $cv->end; |
304 | $cv->end; |
305 | }); |
305 | }); |
306 | } |
306 | } |
307 | $cv->recv; |
307 | $cv->recv; |
308 | |
308 | |
309 | lhead1 CONCEPTS |
309 | CONCEPTS |
310 | |
|
|
311 | This module can create new processes either by executing a new perl |
310 | This module can create new processes either by executing a new perl |
312 | process, or by forking from an existing "template" process. |
311 | process, or by forking from an existing "template" process. |
313 | |
312 | |
314 | All these processes are called "child processes" (whether they are |
313 | All these processes are called "child processes" (whether they are |
315 | direct children or not), while the process that manages them is called |
314 | direct children or not), while the process that manages them is called |
… | |
… | |
440 | You should use "new" whenever possible, except when having a |
439 | You should use "new" whenever possible, except when having a |
441 | template process around is unacceptable. |
440 | template process around is unacceptable. |
442 | |
441 | |
443 | The path to the perl interpreter is divined using various methods - |
442 | The path to the perl interpreter is divined using various methods - |
444 | first $^X is investigated to see if the path ends with something |
443 | first $^X is investigated to see if the path ends with something |
445 | that sounds as if it were the perl interpreter. Failing this, the |
444 | that looks as if it were the perl interpreter. Failing this, the |
446 | module falls back to using $Config::Config{perlpath}. |
445 | module falls back to using $Config::Config{perlpath}. |
|
|
446 | |
|
|
447 | The path to perl can also be overriden by setting the global |
|
|
448 | variable $AnyEvent::Fork::PERL - it's value will be used for all |
|
|
449 | subsequent invocations. |
447 | |
450 | |
448 | $pid = $proc->pid |
451 | $pid = $proc->pid |
449 | Returns the process id of the process *iff it is a direct child of |
452 | Returns the process id of the process *iff it is a direct child of |
450 | the process running AnyEvent::Fork*, and "undef" otherwise. |
453 | the process running AnyEvent::Fork*, and "undef" otherwise. As a |
|
|
454 | general rule (that you cannot rely upon), processes created via |
|
|
455 | "new_exec", AnyEvent::Fork::Early or AnyEvent::Fork::Template are |
|
|
456 | direct children, while all other processes are not. |
451 | |
457 | |
452 | Normally, only processes created via "AnyEvent::Fork->new_exec" and |
458 | Or in other words, you do not normally have to take care of zombies |
453 | AnyEvent::Fork::Template are direct children, and you are |
459 | for processes created via "new", but when in doubt, or zombies are a |
454 | responsible to clean up their zombies when they die. |
460 | problem, you need to check whether a process is a diretc child by |
455 | |
461 | calling this method, and possibly creating a child watcher or reap |
456 | All other processes are not direct children, and will be cleaned up |
462 | it manually. |
457 | by AnyEvent::Fork itself. |
|
|
458 | |
463 | |
459 | $proc = $proc->eval ($perlcode, @args) |
464 | $proc = $proc->eval ($perlcode, @args) |
460 | Evaluates the given $perlcode as ... Perl code, while setting @_ to |
465 | Evaluates the given $perlcode as ... Perl code, while setting @_ to |
461 | the strings specified by @args, in the "main" package. |
466 | the strings specified by @args, in the "main" package. |
462 | |
467 | |
… | |
… | |
544 | Even if not used otherwise, the socket can be a good indicator for |
549 | Even if not used otherwise, the socket can be a good indicator for |
545 | the existence of the process - if the other process exits, you get a |
550 | the existence of the process - if the other process exits, you get a |
546 | readable event on it, because exiting the process closes the socket |
551 | readable event on it, because exiting the process closes the socket |
547 | (if it didn't create any children using fork). |
552 | (if it didn't create any children using fork). |
548 | |
553 | |
|
|
554 | Compatibility to AnyEvent::Fork::Remote |
|
|
555 | If you want to write code that works with both this module and |
|
|
556 | AnyEvent::Fork::Remote, you need to write your code so that it |
|
|
557 | assumes there are two file handles for communications, which |
|
|
558 | might not be unix domain sockets. The "run" function should |
|
|
559 | start like this: |
|
|
560 | |
|
|
561 | sub run { |
|
|
562 | my ($rfh, @args) = @_; # @args is your normal arguments |
|
|
563 | my $wfh = fileno $rfh ? $rfh : *STDOUT; |
|
|
564 | |
|
|
565 | # now use $rfh for reading and $wfh for writing |
|
|
566 | } |
|
|
567 | |
|
|
568 | This checks whether the passed file handle is, in fact, the |
|
|
569 | process "STDIN" handle. If it is, then the function was invoked |
|
|
570 | visa AnyEvent::Fork::Remote, so STDIN should be used for reading |
|
|
571 | and "STDOUT" should be used for writing. |
|
|
572 | |
|
|
573 | In all other cases, the function was called via this module, and |
|
|
574 | there is only one file handle that should be sued for reading |
|
|
575 | and writing. |
|
|
576 | |
549 | Example: create a template for a process pool, pass a few strings, |
577 | Example: create a template for a process pool, pass a few strings, |
550 | some file handles, then fork, pass one more string, and run some |
578 | some file handles, then fork, pass one more string, and run some |
551 | code. |
579 | code. |
552 | |
580 | |
553 | my $pool = AnyEvent::Fork |
581 | my $pool = AnyEvent::Fork |
… | |
… | |
576 | my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; |
604 | my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; |
577 | |
605 | |
578 | print scalar <$fh>; # prints "hi #1\n" and "hi #2\n" in any order |
606 | print scalar <$fh>; # prints "hi #1\n" and "hi #2\n" in any order |
579 | } |
607 | } |
580 | |
608 | |
|
|
609 | EXPERIMENTAL METHODS |
|
|
610 | These methods might go away completely or change behaviour, at any time. |
|
|
611 | |
581 | $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
612 | $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
582 | Flushes all commands out to the process and then calls the callback |
613 | Flushes all commands out to the process and then calls the callback |
583 | with the communications socket. |
614 | with the communications socket. |
584 | |
615 | |
585 | The process object becomes unusable on return from this function - |
616 | The process object becomes unusable on return from this function - |
586 | any further method calls result in undefined behaviour. |
617 | any further method calls result in undefined behaviour. |
587 | |
618 | |
588 | The point of this method is to give you a file handle thta you cna |
619 | The point of this method is to give you a file handle that you can |
589 | pass to another process. In that other process, you can call |
620 | pass to another process. In that other process, you can call |
590 | "new_from_fh AnyEvent::Fork" to create a new "AnyEvent::Fork" object |
621 | "new_from_fh AnyEvent::Fork $fh" to create a new "AnyEvent::Fork" |
591 | from it, thereby effectively passing a fork object to another |
622 | object from it, thereby effectively passing a fork object to another |
592 | process. |
623 | process. |
593 | |
624 | |
594 | new_from_fh AnyEvent::Fork $fh # EXPERIMENTAL, MIGHT BE REMOVED |
625 | new_from_fh AnyEvent::Fork $fh # EXPERIMENTAL, MIGHT BE REMOVED |
595 | Takes a file handle originally rceeived by the "to_fh" method and |
626 | Takes a file handle originally rceeived by the "to_fh" method and |
596 | creates a new "AnyEvent:Fork" object. The child process itself will |
627 | creates a new "AnyEvent:Fork" object. The child process itself will |
… | |
… | |
741 | (part of this distribution). |
772 | (part of this distribution). |
742 | |
773 | |
743 | AnyEvent::Fork::Template, to create a process by forking the main |
774 | AnyEvent::Fork::Template, to create a process by forking the main |
744 | program at a convenient time (part of this distribution). |
775 | program at a convenient time (part of this distribution). |
745 | |
776 | |
|
|
777 | AnyEvent::Fork::Remote, for another way to create processes that is |
|
|
778 | mostly compatible to this module and modules building on top of it, but |
|
|
779 | works better with remote processes. |
|
|
780 | |
746 | AnyEvent::Fork::RPC, for simple RPC to child processes (on CPAN). |
781 | AnyEvent::Fork::RPC, for simple RPC to child processes (on CPAN). |
747 | |
782 | |
748 | AnyEvent::Fork::Pool, for simple worker process pool (on CPAN). |
783 | AnyEvent::Fork::Pool, for simple worker process pool (on CPAN). |
749 | |
784 | |
750 | AUTHOR AND CONTACT INFORMATION |
785 | AUTHOR AND CONTACT INFORMATION |