Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Execute a sub in the background

by egzoti4en (Initiate)
on Feb 01, 2012 at 13:05 UTC ( [id://951208]=perlquestion: print w/replies, xml ) Need Help??

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

Hello guys,

I really really need your help fast.

I'm doing a project and I have no idea how to do something.

Here is the deal.

I have a while cycle where i read every key pressed. When i press 'Enter' i want to execute a sub which has a while cycle in it but also there is a sleep in it.

Here is some example code :

do { if($key eq "\n") #execute sub_example() if($key eq "261) #do something else }while ((key = getch()) ne ERR) sub_example { do {#....... sleep 5 #.... while(1>0)} }

So my issue is that when i execute sub_example the program freezes and waits for the cycle in the sub to finish. I want it to run in the background and still have the possibility to read from the keyboard and do stuff.

Thank you very much for your help i really needed.

Replies are listed 'Best First'.
Re: Execute a sub in the background
by choroba (Cardinal) on Feb 01, 2012 at 13:17 UTC
    Can you give a bigger picture of what you are you trying to achieve? You can use threads, for example, or maybe GUI would be better for your application than command line interface.
    Update: Other options include Coro, fork any many more.

      I tried using threads but they don't execute in the background or i simply am not using them correct.

      for bigger picture you can see the code uploaded on my github account https://github.com/egzoti4en/MPG123FrontEnd.

      I'm trying to count the time that the song has lasted and to draw a status line of it. This what i want to do in this sub and that is why i need a sleep activity to make it sleep and then draw using Curses.(the sub is called status_line()

      I have alredy done almost everything so GUI is not an option for me now

        I tried using threads but they don't execute in the background or i simply am not using them correct.

        You do have to do a little more than put use threads; at the top of your code.

        Which is all you've done in the code you posted the link to. (It is preferred that you post your code here, not some other place on the net.)

        Did you read the POD for the threads module? Is there something you read but didn't understand that we could help you with?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: Execute a sub in the background
by Anonymous Monk on Feb 01, 2012 at 14:58 UTC
    You're doing it the wrong way. Learn about event-driven programming.
    use 5.010; use AnyEvent; use Term::ReadKey qw(ReadMode ReadKey); ReadMode('cbreak'); my %events; %events = ( key => AE::io(*STDIN, 0, sub { for (ReadKey) { when ('q') { ReadMode('normal'); $events{quit}->send; + } when ("\n") { say 'ZOMG, ENTER!'; } default { say } } }), quit => AE::cv, ); $events{quit}->recv;
    ) { ReadMode(

      Well this worked excellent $thr = threads->create(\&status_line)->detach;.

      Thank you very much for this advice.

      Is there a way after the thread is detached to kill the process ?

        You don't have to worry about detached threads when killing the process. They will be taken care of. When a thread started without 'detach'-ing it is still alive when your process is terminated you will get a warning that it exited with unjoined threads. My experience is mainly with Tk: in this case detached threads work fine as far as they never terminate. If they terminate while the MainLoop is still active it will crash miserably. So with Tk I always use infinite while(1){#do processing} style threads.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-24 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found