Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: How to run POE as a daemon

by rah (Monk)
on Oct 01, 2002 at 21:38 UTC ( [id://202122]=note: print w/replies, xml ) Need Help??


in reply to Re: How to run POE as a daemon
in thread How to run POE as a daemon

This is not a daemon:
while (1) { print "Enter a number: "; $Number = <STDIN>; chomp($Number); print "You entered - $Number.\n"; }
yet you would have me believe it is.

When I said daemon, I meant having true daemon characteristics, not kinda sorta daemon-like. Fork and exit, setsid, chdir /, close or redirect STDIN, STDOUT, STDERR. Here's the example from Matt Sergeants' talk:

Daemonizing POE

  • We often write daemons with POE (network daemons, cron daemons, etc
  • But POE doesn't "background" automatically, so we need to write that:
use POE; use POSIX; $|++; sub become_daemon { my $child = fork; die "Can't fork: $!" unless defined($child); exit(0) if $child; # parent dies; POSIX::setsid(); # become session leader open(STDIN,"</dev/null"); open(STDOUT,">/dev/null"); open(STDERR, '>&STDOUT'); umask(0); # forget file mode creation mask $ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; } become_daemon(); # MUST do this before we create any sessions POE::Session->create( inline_states => { _start => sub { $_[KERNEL]->yield("loop") }, loop => sub { $_[KERNEL]->delay_set("loop", 10) }, }); $poe_kernel->run(); exit(0);
This example fails in the same way: POE::Kernel's run() method was never called.

Replies are listed 'Best First'.
Re: Re: Re: How to run POE as a daemon
by Matts (Deacon) on Oct 02, 2002 at 13:16 UTC
    It doesn't fail. The warning "POE::Kernel's run() method was never called" is from the fact that the parent exits without having ever called $poe_kernel->run(), which is absolutely fine because the child continues and does that. It's just a warning. Ignore it. That warning should be gone in POE 0.23.
Re: Re: Re: How to run POE as a daemon
by hossman (Prior) on Oct 02, 2002 at 00:39 UTC
    This is not a daemon:
    while (1) { print "Enter a number: "; $Number = <STDIN>; chomp($Number); print "You entered - $Number.\n"; }
    yet you would have me believe it is.

    No, I don't consider that a daemon because it's actively soliciting information on a handle (or port, or file, etc...) as soon as it starts. I would however consider this to be a very simple daemon...

    while (1) { $Number = <STDIN>; chomp($Number); print "You entered - $Number.\n"; }

    ...because it passively waits for a connection on a handle (or port, etc...) before doing anything. it's not a very useful daemon because it's single threaded ... but lots of daemons can run as single threads.

    But none of this is really helping you achieve what you want, so i guess we'll just agree to disagree.

    In the mean time, perhaps Proc::Daemon would be of interest to you?

Re: Re: Re: How to run POE as a daemon
by Anonymous Monk on Oct 01, 2002 at 22:19 UTC

    I have no idea what POE is but that code is already dodgy to begin with. Perhaps this is a usage I'm not familar with but I'd normally write open(STDERR, '>&STDOUT'); as *STDERR = *STDOUT instead. There are no  or die "Couldn't open ...: $!" tests on the open() calls. It'd be interesting if you'd stick some print() calls around that script and let us know which line it's cheffing on.

      Please log in if you wish to comment on somebody's code looking "dodgy". Yes, this is a usage you're not familiar with. Please read perldoc -f open. The "or die" is skipped because this is for a presentation - it has to fit on a slide and be visual and understandable by the attendees in the few seconds that I have to show it to them. Plus if you can give me a reason they might fail on a unix system I'll be a lot more interested in your comment ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://202122]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-05-28 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found