Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Need help in getting started with POE

by smarthacker67 (Beadle)
on Jun 16, 2020 at 19:08 UTC ( [id://11118148]=perlquestion: print w/replies, xml ) Need Help??

smarthacker67 has asked for the wisdom of the Perl Monks concerning the following question:


Hi Monks, From couple of days, I am doing some research about POE which seems a good candidate to solve my problem.
After searching a lot and trying from code from cookbook Yet not clear on how to write my code can some please help me with sample.
here is what I am looking for.
package MyPackage; while(1) { fetch_data(); send_to_poe(); <<< this how & where I want to send to POE. so th +at while loop can process it should not wait for reply as it don't ne +ed reply so can say an async call }

There will be a script running in an infinite loop that will never end. eg I found this but now want to add own sub that will so that once I receive the event from above code. I can process in the background. How can I add new handler in the following code will a new key eg _my_function => \&get_work_done will work. Sorry completely new to POE concept.
POE::Session->create( inline_states => { _start => sub { $_[KERNEL]->yield("next") }, next => sub { print "listening...\n"; $_[KERNEL]->delay(next => 1); }, }, ); POE::Kernel->run(); # exit

* I wish to run a script in continue never-ending loop unless user kills it lets call it Worker.
* want to send some work i.e few parameters to worker eg host and command.

Replies are listed 'Best First'.
Re: Need help in getting started with POE
by Fletch (Bishop) on Jun 17, 2020 at 04:30 UTC

    In general when using POE (or another event loop) you're not going to be writing a while loop like that. It's a different mindset where you've got to turn your traditional imperative thinking sort of inside out. Rather than saying "Do X, then do Y, then do Z, then exit" instead you set up handlers you want called when certain things happen (network connections are received, data comes in on a socket, a filehandle becomes readable, . . .), wire things up (in POE you'd do this (say) by making a POE::Session instance mapping with inline_states from event names to your subs), and then you start the event loop and let it take over. When something you've expressed interest happens, the event loop cals your code which does its thing and then returns control back to the event loop.

    Without a bit more detail what exactly your fetch_data is doing it's going to be hard to give a more specific example how to hook things up (or if you're lucky, point you at an existing module), but there's several samples in the POE distribution which might help steer you the right way; e.g. examples/queue.perl. There's also the POE Cookbook which has a good smattering of samples as well.

    Edit: wording.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thank you for you response Sir! So in short I will need some medium to send data I was under the assumption that it will get transmitted directly to session we are calling.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-25 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found