Hello,

I am trying to set up a simple job queue that will allow tasks to be worked on in parallel from different processes. I am quite new to Perl so I may have missed something obvious that I should be doing.

I have used IPC::DirQueue, and this works well when I have one 'worker' task, from the cpan page http://search.cpan.org/~jmason/IPC-DirQueue-1.0/lib/IPC/DirQueue.pm it looks like exactly the solution I need.

When I try to run two or more worker tasks, occasionally more than one worker will pick up the same job.

This is a problem for efficiency, each task is quite long, as well as causing errors. As once two workers get the same job one of the workers will then have an error when trying to assess the jobs data file or when trying to set the job to finished, because the other worker has finished the job first, so the files have been deleted.

Error: IPC::DirQueue: unlink failed: DirQueueTest/active/50.20140528105122431255.DNzE2 at C:/Perl 64/site/lib/IPC/DirQueue.pm line 831.

The following short code causes the problem for me: worker.pl (is run twice, or more)

use strict; use warnings; use IPC::DirQueue; use IPC::DirQueue::Job; my $dq = IPC::DirQueue->new({ dir => 'DirQueueTest' }); while(1) { my $job = $dq->wait_for_queued_job(0); if (!$job) { print "no jobs left\n"; } else { my $TestNumber = $job->{metadata}->{TestNumber}; my $QueuePid = $job->{metadata}->{QueuePid}; print "Worker:$$ Running QueuePid=$QueuePid, TestNum=$TestNumber\n +"; $job->finish(); } }
queue.pl (is run once)
use strict; use warnings; use IPC::DirQueue; my $DirQueueOpts = { dir => 'DirQueueTest', active_file_lifetime => (60*60*24) }; my $TestNum = 0; while($TestNum < 100000) { print "QueuePid=$$, TestNum=$TestNum\n"; my $dq = IPC::DirQueue->new($DirQueueOpts); my $QNXBuildMetaData = { TestNumber => $TestNum, QueuePid => $$}; $dq->enqueue_string("QueuePid=$$, TestNum=$TestNum", $QNXBuildMeta +Data); $TestNum = $TestNum+1; }

I have also looked at using Directory::Queue::Simple but this seems to have a similar problem. Sometimes the $dirq->lock($name); function works and returns false when the job has been locked by the other worker. But sometimes it returns true, and both workers start the same job.

I am using ActiveState Perl v5.16.3 build 1604

Is this a known problem with locking files on windows platforms? Or should I be doing some error checking that i have not understood?

Any suggestions for alternative approaches to this problem are also welcome, but I was hoping to get away without having to run a server based message queue or database.


In reply to Problems using file based queues on windows by joe425

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.