Update: Added missing line after looping each region.

Update: This example runs nearly as fast as serial code and completes in 14.5 seconds without threads.

The following demo demonstrates multiple workers with a single writer running simultaneously. Locking between workers is handled automatically by MCE.

use strict; use warnings; use Excel::Writer::XLSX; use MCE::Loop Sereal => 1; my $nodeList = [ [ 'AMS' , 'a' ], [ 'APJ' , 'ap' ], [ 'EMEA', 'e' ], ]; my ($workbook, %worksheets, $format); $workbook = Excel::Writer::XLSX->new("Node_List.xlsx"); $workbook->set_properties( title => 'Node List', author => 'L_WC demo', comments => 'Node List', ); $format = $workbook->add_format(bg_color => 44); $format->set_align('center'); foreach (@{ $nodeList }) { $worksheets{ $_->[0] } = $workbook->add_worksheet( $_->[0] ); } MCE::Loop::init( chunk_size => 1, max_workers => scalar(@{ $nodeList }), gather => sub { my $region = shift; while (@{ $_[0] }) { my @args = splice(@{ $_[0] }, 0, 3); $worksheets{$region}->write(@args, $format) } }, ); mce_loop { doSomething($_->[0], $_->[1]) } $nodeList; $workbook->close(); print "Node List is Done.\n"; sub doSomething { my ($region, $sql) = @_; if ($region eq 'AMS') { my @data; my $n = $region . '_'; for (0..65534) { push @data, $_, 0, $n . $_; push @data, $_, 1, $_ + 4; push @data, $_, 2, $_ + 3; push @data, $_, 3, $_ + 2; push @data, $_, 4, $_ + 1; if ($_ % 4000 == 0) { MCE->gather($region, \@data); @data = (); } } MCE->gather($region, \@data) if @data; print "AMS -----DONE.\n"; } elsif ($region eq 'APJ') { my @data; my $n = $region . '_'; for (0..65534) { push @data, $_, 0, $n . $_; push @data, $_, 1, $_ + 1; push @data, $_, 2, $_ + 3; push @data, $_, 3, $_ + 4; push @data, $_, 4, $_ + 2; if ($_ % 4000 == 0) { MCE->gather($region, \@data); @data = (); } } MCE->gather($region, \@data) if @data; print "APJ -----DONE.\n"; } elsif ($region eq 'EMEA') { my @data; my $n = $region . '_'; for (0..65534) { push @data, $_, 0, $n . $_; push @data, $_, 1, $_ + 1; push @data, $_, 2, $_ + 2; push @data, $_, 3, $_ + 3; push @data, $_, 4, $_ + 4; if ($_ % 4000 == 0) { MCE->gather($region, \@data); @data = (); } } MCE->gather($region, \@data) if @data; print "EMEA -----DONE.\n"; } return; }

Kind regards, Mario


In reply to Re: How to use threads to write worksheets of excel by marioroy
in thread How to use threads to write worksheets of excel by L_WC

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.