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

Hello Everyone,

I wanted to use eval on threads. I want to catch and restart thread.

This is what I have tried and it does not work. I open file that can't be opened and the thread dies. Is there a better way to do this. All I want to do is thread to log error and restart if something happened.

my $thr = threads->create ( sub { while (1) { eval { while (1) { worker(); } }; if ($@) { print "Error Occured: <$@>.\n"; $@=(); sleep (1); } } } ) sub worker { open FILE_OUT, "> /root/xyz" or die "Can't open file: $!"; }

Replies are listed 'Best First'.
Re: Eval thread and restart it.
by BrowserUk (Patriarch) on Apr 19, 2010 at 20:01 UTC

    The concept works fine:

    perl -Mthreads -e"threads->create(sub{{eval{sleep rand 10;die time};$@ and print "$@" +;redo}})->join" 1271707092 at -e line 1. 1271707094 at -e line 1. 1271707100 at -e line 1. 1271707108 at -e line 1. Terminating on signal SIGINT(2)

    So you're going to have to show us what you are doing that is causing the permission denied death if we are to help you.


    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.
Re: Eval thread and restart it.
by Corion (Patriarch) on Apr 19, 2010 at 19:39 UTC

    So, how does it not work for you?

      The thread just exists after printing "Error Occured: Permission denied at x.pl line 20."

        Your code as posted doesn't even compile. When I fix the code as follows, it works for me. So it must be your old version of threads, or your version of Perl, or maybe you are not showing us the real code you use.

        use threads; use strict; my $thr = threads->create ( sub { while (1) { eval { while (1) { worker(); } }; if ($@) { print "Error: <$@>.\n"; print "Error Occured: <$@>.\n"; $@=(); sleep (1); } } } ); sub worker { open FILE_OUT, "> /root/xyz" or die "Can't open file: $!"; } $thr->join; __END__ Name "main::FILE_OUT" used only once: possible typo at tmp.pl line 27. Error: <Can't open file: No such file or directory at tmp.pl line 27. >. Error Occured: <Can't open file: No such file or directory at tmp.pl l +ine 27. >. Error: <Can't open file: No such file or directory at tmp.pl line 27. >. Error Occured: <Can't open file: No such file or directory at tmp.pl l +ine 27. >. Error: <Can't open file: No such file or directory at tmp.pl line 27. >. Error Occured: <Can't open file: No such file or directory at tmp.pl l +ine 27. >. Terminating on signal SIGINT(2)
        C:\Projekte>perl -V Summary of my perl5 (revision 5 version 10 subversion 0) configuration +: Platform: osname=MSWin32, osvers=5.1, archname=MSWin32-x86-multi-thread uname='' config_args='undef' hint=recommended, useposix=true, d_sigaction=undef useithreads=define, usemultiplicity=define useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=und +ef use64bitint=undef, use64bitall=undef, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='gcc', ccflags =' -s -O2 -DWIN32 -DHAVE_DES_FCRYPT -DPERL_IMPL +ICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-strict-aliasing -DPERL_M SVCRT_READFIX', optimize='-s -O2', cppflags='-DWIN32' ccversion='', gccversion='3.4.5', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=12 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='long lo +ng', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='g++', ldflags ='-s -L"C:\strawberry\perl\lib\CORE" -L"C:\straw +berry\c\lib"' libpth=C:\strawberry\c\lib libs= -lmsvcrt -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool - +lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 - luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 perllibs= -lmsvcrt -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspo +ol -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi 32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 libc=-lmsvcrt, so=dll, useshrplib=true, libperl=libperl510.a gnulibc_version='' Dynamic Linking: dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags='-mdll -s -L"C:\strawberry\perl\lib\CORE +" -L"C:\strawberry\c\lib"' Characteristics of this binary (from libperl): Compile-time options: MULTIPLICITY PERL_DONT_CREATE_GVSV PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS PERL_MALLOC_WRAP PL_OP_SLAB_ALLOC USE_ITHREADS USE_LARGE_FILES USE_PERLIO Built under MSWin32 Compiled at Aug 11 2008 04:41:33 @INC: C:/strawberry/perl/lib C:/strawberry/perl/site/lib . C:\Projekte>perl -Mthreads -e "die $threads::VERSION" 1.73 at -e line 1.