develooper Front page | perl.ithreads | Postings from March 2008

Time to cancel a thread

Thread Next
From:
Javier Terceiro
Date:
March 24, 2008 16:24
Subject:
Time to cancel a thread
Message ID:
602eb19e0803241624t2adb064alad4e66b98c515548@mail.gmail.com
Hello list,

I am writing a daemon. I have a daemon that it check a folder and then
throws both threads exist as files. It's well, the problem is that
each thread need it ends at a certain time (defined by default). If
this does not complete the work to be cancelled. For example, if it is
making a connection to a website and it does not respond in 2 minutes,
until the process completes and the thread too, running a particular
code before.

I was looking like doing and I did not find anything. Any thoughts on
how to do that

My source code is:

#!/usr/bin/perl -w

use strict;
use warnings;
use AnyData;
use Sys::Syslog qw(:standard :macros);
use POSIX qw(setsid);
use threads;
use threads::shared;

sub mail
{
        sleep(2);
        syslog LOG_INFO, "thread for mail";
        my $file_name = shift;
	...
	# If you spent 2 min, canceled
	# source cancel
	syslog LOG_INFO, "Surpassed time";

	# else
	syslog LOG_INFO, "Process completed";
}

sub scan_files
{
        my $DIR = "workq/";
        while(1)
        {
                opendir(DIRHANDLE, $DIR) || die "ERROR: $DIR not read\n";
                foreach (readdir(DIRHANDLE))
                {
                        if (!-d $_)
                        {
				threads->new(\&mail, $_);
                        }
                }
                closedir DIRHANDLE;
                sleep(10);
        }
}
...
...
...

-- 
A greeting,

Javier.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About