I greatly appreciate the response from everyone regarding my threads.
After 2 days of research I don't know if what Im trying to do is even
possible but I've started down this road and with a project deadline its
difficult to just start over with another language so I am looking to
exploit threads to fullest capacity.  

Some of you have asked for code examples.

One of issues that I am having is when the thread enters into
getAvailableResource routine its not seeing that the
$Resources{'WAN1'}->{INUSE} = 1; has been made unavailable.  The idea is
for the next thread to pick up the next unavailable resource.  I tried
to sleep(1) between thread creation, and also used threads::shared
share(%Resources); which also failed.  

I am new to threads in PERL.  Ive had great success with JAVA threads.
This program is a bit of beast with all the things that it has to do
before getting to the end result.  

This code will eventually be turned into a win32::service but only after
I get it debugged and working correctly.  

I greatly appreciate everyones time in offering me advice in this
situation.

Thanks, 
Dan J. Rychlik
IT Projects Engineer

Sample code::

#! c:\perl\bin\perl.exe

use strict;
use DBI;
use threads;
use threads::shared;
use File::Path;
use Win32::EventLog;
use Win32::FileOp;
use Win32::OLE;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use LWP::Simple;

our (@sitePushData,$dbh);

# List of directories to create upon install...
my @Directories = ('c:\\DJ\\SITE_UPDATE\\LOG_DATA\\',
                   'c:\\DJ\\SITE_UPDATE\\QUEUE\\',
                   'c:\\DJ\\SITE_UPDATE\\SPOOL\\',
                   'c:\\DJ\\SITE_UPDATE\\COMPLETE\\',
                   'c:\\DJ\\BIN\\'
                   );

# Runtime Database for SITE ID Tracking 
our %RunTimeKey;

# Runtime Resource handling
our %Resources;

# WAN Resources
$Resources{'WAN1'} = {'DRIVE' => 'O:', 'INUSE' => 0};
$Resources{'WAN2'} = {'DRIVE' => 'P:', 'INUSE' => 0};
$Resources{'WAN3'} = {'DRIVE' => 'Q:', 'INUSE' => 0};
$Resources{'WAN4'} = {'DRIVE' => 'R:', 'INUSE' => 0};
$Resources{'WAN5'} = {'DRIVE' => 'S:', 'INUSE' => 0};
$Resources{'WAN6'} = {'DRIVE' => 'T:', 'INUSE' => 0};

# MODEM Resources
$Resources{'POLL1'} = {'DRIVE' => 'U:', 'INUSE' => 0};
$Resources{'POLL2'} = {'DRIVE' => 'V:', 'INUSE' => 0};
$Resources{'POLL3'} = {'DRIVE' => 'W:', 'INUSE' => 0};
$Resources{'POLL4'} = {'DRIVE' => 'X:', 'INUSE' => 0};
$Resources{'POLL5'} = {'DRIVE' => 'Y:', 'INUSE' => 0};
$Resources{'POLL6'} = {'DRIVE' => 'Z:', 'INUSE' => 0};

share(%Resources);

my ($file,$thread);

while (1) {

    eval { mkpath([EMAIL PROTECTED], 1, 0777) };
    if ($@) {
        print "Could not create: $@";
    }

    opendir (QUEUE, $Directories[1]) or die "Unable to open $!\n";
 
    while (defined ($file = readdir (QUEUE)) ) {

        if ($file =~ m/^\d{5}/) {

            prepPackage($file);
        }
    }
    
    closedir(QUEUE);

    foreach my $rec (keys %RunTimeKey) {
    
        $thread  = threads->create("getAvailableResource","$rec");
        $thread->detach();

        sleep(1);
            
    }

    print "All Quiet: Waiting for files\n";
    sleep (300);
    
}

    
  

#-----------------------------------------------------------------------
-----
# Routine that handles the assignment of resources so that multitasking
is
# made useful
#-----------------------------------------------------------------------
-----
sub getAvailableResource {
    
    my ($siteId) = @_;
    
    # If its WAN
    if ($RunTimeKey{$siteId}->{'wan'} == 1) {
    
          # If its WAN1 resource, mark it INUSE and so on and so
forth...
        
          if ($Resources{'WAN1'}->{INUSE} == 0) {    
            $Resources{'WAN1'}->{INUSE} = 1;
            dataHandler('WAN1',$siteId);
            $Resources{'WAN1'}->{INUSE} = 0;
        }
        elsif ($Resources{'WAN2'}->{INUSE} == 0) {
            $Resources{'WAN2'}->{INUSE} = 1;
            dataHandler('WAN2',$siteId);
            $Resources{'WAN2'}->{INUSE} = 0;            
        }
        elsif ($Resources{'WAN3'}->{INUSE} == 0) {
            $Resources{'WAN3'}->{INUSE} = 1;
            dataHandler('WAN3',$siteId);
            $Resources{'WAN3'}->{INUSE} = 0;            
        }
        elsif ($Resources{'WAN4'}->{INUSE} == 0) {
            $Resources{'WAN4'}->{INUSE} = 1;
            dataHandler('WAN4',$siteId);
            $Resources{'WAN4'}->{INUSE} = 0;              
        }
        elsif ($Resources{'WAN5'}->{INUSE} == 0) {
            $Resources{'WAN5'}->{INUSE} = 1;
            dataHandler('WAN5',$siteId);
            $Resources{'WAN5'}->{INUSE} = 0;              
        }

        # If its NOT on the WAN make a modem call

    } elsif ($RunTimeKey{$siteId}->{'wan'} == 0) {

        if ($Resources{'POLL1'}->{INUSE} == 0) {    
            $Resources{'POLL1'}->{INUSE} = 1;
            useModem($siteId);
            $Resources{'POLL1'}->{INUSE} = 0;
        }
        elsif ($Resources{'POLL2'}->{INUSE} == 0) {
            $Resources{'POLL2'}->{INUSE} = 1;
            useModem($siteId);
            $Resources{'POLL2'}->{INUSE} = 0;            
        }
        elsif ($Resources{'POLL3'}->{INUSE} == 0) {
            $Resources{'POLL3'}->{INUSE} = 1;
            useModem($siteId);
            $Resources{'POLL3'}->{INUSE} = 0;            
        }
        elsif ($Resources{'POLL4'}->{INUSE} == 0) {
            $Resources{'POLL4'}->{INUSE} = 1;
            useModem($siteId);
            $Resources{'POLL4'}->{INUSE} = 0;              
        }
        elsif ($Resources{'POLL5'}->{INUSE} == 0) {
            $Resources{'POLL5'}->{INUSE} = 1;
            useModem($siteId);
            $Resources{'POLL5'}->{INUSE} = 0;              
        }        
    }
}

#-----------------------------------------------------------------------
-----
# Routine that handles Modem Transfers
#-----------------------------------------------------------------------
-----
sub useModem {
    my ($siteId) = @_;
    
    print "Dialing modem for $siteId\n";
    
    return;    
    
}

#-----------------------------------------------------------------------
-----
# Routine that maps the drive and copies the data
# Incomplete, due to Thread issues.
#-----------------------------------------------------------------------
-----
sub dataHandler {
    
    my ($resource,$siteId) = @_;
    
    print $resource." <- COMING IN\n";
    
    my $LocalName = $Resources{$resource}->{DRIVE};
    my $RemoteName = "\\\\".$RunTimeKey{$siteId}->{ip}."\\root";
    my $UpdateProfile = 0;
    my $User = $RunTimeKey{$siteId}->{userid};
    my $Password = $RunTimeKey{$siteId}->{password};
    my $Force = 1;

    print $LocalName." localname\n";
    print $RemoteName." remote name\n";
    print $UpdateProfile." updateprofile\n";
    print $User." username\n";
    print $Password." password\n";
    print $Force." force\n";


    my $objnet=Win32::OLE->CreateObject("Wscript.Network");

    $objnet->MapNetworkDrive($LocalName, $RemoteName, $UpdateProfile,
$User, $Password);

    print Win32::OLE->LastError();

    sleep (10);

    $objnet->RemoveNetworkDrive($LocalName, $Force, $UpdateProfile);
    
    return;
    
}



-----Original Message-----
From: Daniel Rychlik [mailto:[EMAIL PROTECTED] 
Sent: Friday, April 14, 2006 10:28 AM
To: [email protected]
Subject: iThreads troubles

Good morning,

 

I am having troubles with threads within my PERL service which was built
to monitor a queue for jobs to do.  The issue I am running into is, I
have an unknown number of jobs to do but can only do 6 at one time.  The
reason that I can only do 6 at one time is because I only have 6
resources available to me for each job.  

 

I looked into using threads to do the jobs asynchronously, as a file
comes into a queue; a loop monitors this queue and then creates a new
thread to carry out that job.  I have this piece of the code complete,
the problem is after the job is complete and a list of instructions is
complete, the thread doesn't clean up after itself.  My thread count
continues to increment as well as memory space.  The loop that I use to
monitor the queue is a while true loop and it sleeps for 2 minutes
waiting for more files to process and upload.  

 

I need help on how to clean up after a thread to release its resource.
I have researched this for 2 days now and Im growing weary.  It was
suggested to me to subscribe to this list to find out more information.


 

I greatly appreciate this in advance,

Dan J. Rychlik

Reply via email to