threads, Sys::SigAction, alarm, timeout
threads, Sys::SigAction, alarm, timeout
am 08.10.2007 14:17:48 von LovingFox
I've got some kind of problem using threads module with Sys::SigAction
module. Look to this too examples:
1. The first (function 't' w/o threads) works good as I need:
------------------------------------------------------------ -------------
#!/usr/bin/perl -w
use strict;
use Sys::SigAction qw( set_sig_handler );
t();
sub t {
eval {
set_sig_handler( 'ALRM', sub { die 'alarm!' } );
alarm(1);
sleep(2); # here is some usefull code instead 'sleep'
alarm(0);
};
print "end '$@'\n";
}
------------------------------------------------------------ -------------
2. The second (function 't' through threads) prints error I cannot
understand:
------------------------------------------------------------ -------------
#!/usr/bin/perl -w
use strict;
use threads;
use Sys::SigAction qw( set_sig_handler timeout_call );
threads->new( \&t )->join();
sub t {
eval {
set_sig_handler( 'ALRM', sub { die 'alarm!' } );
alarm(1);
sleep(2);
alarm(0);
};
print "end '$@'\n";
}
------------------------------------------------------------ -------------
This code print this:
Signal SIGALRM received, but no signal handler set.
How it can be overcome?
Re: threads, Sys::SigAction, alarm, timeout
am 08.10.2007 17:12:56 von zentara
On Mon, 08 Oct 2007 12:17:48 -0000, "LovingFox@gmail.com"
wrote:
>I've got some kind of problem using threads module with Sys::SigAction
>module. Look to this too examples:
>
>1. The first (function 't' w/o threads) works good as I need:
>----------------------------------------------------------- --------------
>#!/usr/bin/perl -w
>use strict;
>use Sys::SigAction qw( set_sig_handler );
>t();
>sub t {
> eval {
> set_sig_handler( 'ALRM', sub { die 'alarm!' } );
> alarm(1);
> sleep(2); # here is some usefull code instead 'sleep'
> alarm(0);
> };
> print "end '$@'\n";
>}
>----------------------------------------------------------- --------------
>
>2. The second (function 't' through threads) prints error I cannot
>understand:
>----------------------------------------------------------- --------------
>#!/usr/bin/perl -w
>use strict;
>use threads;
>use Sys::SigAction qw( set_sig_handler timeout_call );
>threads->new( \&t )->join();
>sub t {
> eval {
> set_sig_handler( 'ALRM', sub { die 'alarm!' } );
> alarm(1);
> sleep(2);
> alarm(0);
> };
> print "end '$@'\n";
>}
>----------------------------------------------------------- --------------
>This code print this:
>Signal SIGALRM received, but no signal handler set.
>
>How it can be overcome?
I don't know about how Sys::Sigaction works, but threads and ALARM
and/or signals don't work with threads. The parent thread receives all
signals, so using them in threads is useless unless you have the parent
handle them.
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Re: threads, Sys::SigAction, alarm, timeout
am 08.10.2007 22:03:59 von Martijn Lievaart
On Mon, 08 Oct 2007 11:12:56 -0400, zentara wrote:
> I don't know about how Sys::Sigaction works, but threads and ALARM
> and/or signals don't work with threads. The parent thread receives all
> signals, so using them in threads is useless unless you have the parent
> handle them.
>
> zentara
from perlthrtut:
] Similarly, mixing signals and threads should not be attempted.
] Implementations are platform-dependent, and even the POSIX semantics
] may not be what you expect (and Perl doesnât even give you the full
] POSIX API).
So I would not even count on the main thread receiving all signals. Or am
I missing something here?
M4
Re: threads, Sys::SigAction, alarm, timeout
am 09.10.2007 13:19:42 von LovingFox
On 9 , 00:03, Martijn Lievaart wrote:
> On Mon, 08 Oct 2007 11:12:56 -0400, zentara wrote:
> > I don't know about how Sys::Sigaction works, but threads and ALARM
> > and/or signals don't work with threads. The parent thread receives all
> > signals, so using them in threads is useless unless you have the parent
> > handle them.
>
> > zentara
>
> from perlthrtut:
>
> ] Similarly, mixing signals and threads should not be attempted.
> ] Implementations are platform-dependent, and even the POSIX semantics
> ] may not be what you expect (and Perl doesn't even give you the full
> ] POSIX API).
>
> So I would not even count on the main thread receiving all signals. Or am
> I missing something here?
>
> M4
Hmmm... So anybody know how to create timeout catcher for some code in
thread script like I wrote? Such simple example is written here
http://search.cpan.org/~lbaxter/Sys-SigAction-0.10/dbd-oracl e-timeout.POD
and I need to use it in code with threads.
Re: threads, Sys::SigAction, alarm, timeout
am 11.10.2007 04:31:38 von lincoln.a.baxter
On Oct 9, 7:19 am, "Loving...@gmail.com" wrote:
> On 9 , 00:03, Martijn Lievaart wrote:
>
>
>
> > On Mon, 08 Oct 2007 11:12:56 -0400, zentara wrote:
> > > I don't know about how Sys::Sigaction works, but threads and ALARM
> > > and/or signals don't work with threads. The parent thread receives all
> > > signals, so using them in threads is useless unless you have the parent
> > > handle them.
>
> > > zentara
>
> > from perlthrtut:
>
> > ] Similarly, mixing signals and threads should not be attempted.
> > ] Implementations are platform-dependent, and even the POSIX semantics
> > ] may not be what you expect (and Perl doesn't even give you the full
> > ] POSIX API).
>
> > So I would not even count on the main thread receiving all signals. Or am
> > I missing something here?
>
> > M4
>
> Hmmm... So anybody know how to create timeout catcher for some code in
> thread script like I wrote? Such simple example is written herehttp://search.cpan.org/~lbaxter/Sys-SigAction-0.10/dbd-o racle-timeout...
> and I need to use it in code with threads.
I am the author of the sys-sigaction module. Referenced here. I have
privately corresponded with a person I think is the original poster of
this thread. I believe Martijn Lievaart in the post above has nailed
it with his quote from the from perlthrtut:
>
> > ] Similarly, mixing signals and threads should not be attempted.
> > ] Implementations are platform-dependent, and even the POSIX semantics
> > ] may not be what you expect (and Perl doesn't even give you the full
> > ] POSIX API).
Until perl threads (and the POSIX sigaction API) support this, there
is very little that I think I can do with sys-sigaction to support
it. I have offered to produce a documentation update to sys-sigaction
calling this out, perhaps to at least warn others to avoid trying to
do this. I think one is stuck with not using threads if you wants to
use the technique referenced in sys-sigaction for timing out hung
oracle connection attempts. This post should close this topic with
respect to sys-sigaction, for now.
Re: threads, Sys::SigAction, alarm, timeout
am 11.10.2007 16:52:20 von zentara
On Thu, 11 Oct 2007 02:31:38 -0000, lincoln.a.baxter@gmail.com wrote:
>On Oct 9, 7:19 am, "Loving...@gmail.com" wrote:
>> On 9 , 00:03, Martijn Lievaart wrote:
>>
>>
>>
>> > On Mon, 08 Oct 2007 11:12:56 -0400, zentara wrote:
>> > > I don't know about how Sys::Sigaction works, but threads and ALARM
>> > > and/or signals don't work with threads. The parent thread receives all
>> > > signals, so using them in threads is useless unless you have the parent
>> > > handle them.
>> > > zentara
>> Hmmm... So anybody know how to create timeout catcher for some code in
>> thread script like I wrote? Such simple example is written herehttp://search.cpan.org/~lbaxter/Sys-SigAction-0.10/dbd-o racle-timeout...
>> and I need to use it in code with threads.
What you do is create a separate thread to be a timer, and use shared
variables to communicate when the timeout occurred.
It helps if you have an eventloop system going like Glib, POE,
Tk, etc. That way you can schedule a callback every 10 ms to
see if the timer shared variable has expired. Otherwise you
will need to setup a while loop (which can get complicated or
even impossible).
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html