DreamT has asked for the wisdom of the Perl Monks concerning the following question:
use XML::Writer; use IO::File; use threads; use strict; my $output = new IO::File(">output.xml"); my $writer = new XML::Writer(OUTPUT => $output, DATA_MODE=>1, DATA_IND +ENT=>1,CHECK_PRINT=>1); $writer->xmlDecl("UTF-8"); $writer->startTag('operations'); my $thr1 = threads->new(\&writeArticleData1); my $thr2 = threads->new(\&writeArticleData2); while (!(($thr1->join) && ($thr2->join))) { # Wait for threads to finish } $writer->endTag("operations"); $writer->end(); $output->close(); sub writeArticleData1() { lock($writer); $writer->startTag('product'); $writer->characters("Hello, world!"); $writer->endTag("product"); sleep 2; return 1; } sub writeArticleData2() { lock($writer); $writer->startTag('product'); $writer->characters("Hello, world2!"); $writer->endTag("product"); return 1; } 1;
<?xml version="1.0" encoding="UTF-8"?> <operations> <product>Hello, world2!</product> <product>Hello, world!</product></operations>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multithreaded xml writing using XML::Writer
by BrowserUk (Patriarch) on May 03, 2010 at 11:25 UTC | |
by DreamT (Pilgrim) on May 03, 2010 at 11:47 UTC | |
by Corion (Patriarch) on May 03, 2010 at 11:51 UTC | |
by DreamT (Pilgrim) on May 03, 2010 at 12:02 UTC | |
by BrowserUk (Patriarch) on May 03, 2010 at 12:02 UTC | |
by DreamT (Pilgrim) on May 03, 2010 at 12:18 UTC | |
by BrowserUk (Patriarch) on May 03, 2010 at 12:22 UTC | |
|