Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

ctrl + c on Win32 (signal handling)

by remiah (Hermit)
on Aug 26, 2011 at 13:26 UTC ( [id://922597]=perlquestion: print w/replies, xml ) Need Help??

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

Hello PerlMonk.

I have console perl program that connects to database, checking records every 30 seconds, and if data has changed it should do it's job. With ctrl + c, I want this program to disconnect from database, so I tried SIGINT signal handler even though Chapter 4 of "Advanced Perl Programming" warned me the trouble of Win32 signal. My program shows no response to ctrl + c while sleeping 30 seconds and this is my problem.

So far, I have read through previous threads, SOLVED Perl SIG INT handling conundrum. , GOTO, Signals and Win32 , and especially this one win32, ctrl-c, sleep, and signals .
"set PERL_SIGNALS=unsafe" gives me quick response but it yields fatal error dialog saying perl has crashed. I am using perl 5.12.1 of Win32 IndigoAMPP package. Below is my sample code.
use strict; use warnings; #this doesn't work #$ENV{PERL_SIGNALS} = "unsafe"; local $SIG{INT} =sub { print "in SIGINT ...exitting\n"; #here I want disconnect from database; exit; }; while (1){ print "in while loop\n"; sleep 30; #this is what I can do best so far... #sleep 3 for (0 .. 10); }
I am glad with any advice.

Replies are listed 'Best First'.
Re: ctrl + c on Win32 (signal handling)
by BrowserUk (Patriarch) on Aug 26, 2011 at 13:55 UTC
    #this is what I can do best so far... #sleep 3 for (0 .. 10);

    Given the limited function of the sample code, that is the best solution. Though I'd go for:

    1 while sleep 1;

    Waking up once a second will improve your response time and cause negligible extra cpu usage.

    But I suspect that in your real code you are wanting to do more in the while than just sleep?


    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.
      Waking up once a second will improve your response time and cause negligible extra cpu usage.
      I see... I can get quicker resonse. This seems good compromize for me.
      But I suspect that in your real code you are wanting to do more in the while than just sleep?
      Yes. What I do in real "while loop" is like this.

      1. sleep 30 seconds
      2. check database
      3. image conversions if there is records


      Thanks for your advice.
        I found Term::ReadKey can capture ctrl + c on windows
        use strict; use warnings; use Term::ReadKey; ReadMode "raw"; #raw can capture ctrl + c while(1){ print "prompt>\n"; #sleep 30 seconds my $char = ReadKey 30; next if (! defined $char); if (ord $char == 3 ){ print "control + c\n"; last; } #check database #process image } ReadMode "restore";
        If I pressed ctrl + c while processing something, it will abort. So if I could write like this... (there seems no such function like "BindEvent").
        ReadMode "raw"; #raw can capture ctrl + c BindEvent "keypress", sub { my ($keycode) = @_; if ($keycode == 3){ ReadMode "restore"; #disconnect database exit; } }; #main loop while(1){ ReadKey 30; #check database #process images } ReadMode "restore";
        regards.

        updated:
        I read about Glib main loop,  Re: Perl Hotkey/Shortcut. I'll try it after I sleep several hours;
Re: ctrl + c on Win32 (signal handling)
by sundialsvc4 (Abbot) on Aug 26, 2011 at 13:37 UTC

    Personally, I get nervous about “signals” especially where databases are involved.   (A signal can come in at any time, which seems to always mean, the worst time.

    Could this requirement not be addressed equally well, simply by having the program disconnect from the database before it sleeps for 30 seconds, then reconnect again?

      My program converts images on consle with ImageMagick while I 'm selecting proper image on database application. So I don't want to wait the conversion process while I am working to select proper image.

      After reading your comments, one thing I can think of... to create little Tk widget with quit button and disconnect from database with callback function or widget destroy event.

      ctrl + c is very handy for me, I would like to have some kind of key press event handler like solution for console, if I could...

      Thnaks for your reply.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found