in reply to Re^2: How to use threads to write worksheets of excel
in thread How to use threads to write worksheets of excel
Excel::Writer::XLSX isn't thread-safe, so I'd tackle the problem this way:
#! perl -slw use strict; use threads; use Thread::Queue; use Excel::Writer::XLSX;; sub worker { my( $Q, $region, $sql ) = @_; my $wb = Excel::Writer::XLSX->new("$region.xlsx"); $wb->set_properties( title => 'Node List', author => 'L_WC de +mo', comments => 'Node List' ); $wb->set_optimization(); my $fmt = $wb->add_format( bg_color => 44 ); $fmt->set_align('cent +er'); my $ws = $wb->add_worksheet( $region ); my $row = 0; while( $Q->dequeue ) { $ws->write( $row++, 0, $_ ); } $wb->close; } my @nodeList = ( ['AMS', 'a'], ['APJ', 'ap'], ['EMEA', 'e'] ); my @Qs = map Thread::Queue->new, 1 .. 3; my @threads = map threads->new( \&worker, $Qs[ $_ ], @{ $nodeList[ $_ +] } ), 0 .. 2; for( 1 .. 1e6 ) { my $sheet = int( rand 3 ); $Qs[ $sheet ]->enqueue( $nodeList[ $sheet ][1] . $_ ); } $Qs[ $_ ]->enqueue( undef ) for 0 .. 2; $_->join for @threads;
That writes 1 million items split between 3 workbooks in around 1 minute. Once the 3 files are produced, it should be simple to merge the single sheets from the 3 zipfiles into one workbook by using Excel itself. (I don't have Excel, so I couldn't test that hypothesis.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: How to use threads to write worksheets of excel
by marioroy (Prior) on Jul 01, 2015 at 13:46 UTC | |
by BrowserUk (Patriarch) on Jul 01, 2015 at 22:50 UTC |