KarlCiaran has asked for the wisdom of the Perl Monks concerning the following question:

Monastery: I am trying to create an excel worksheet using CPAN's writeexcel module. The script works as long as I dont use threads. The moment I try the function through a thread the following happens. 1)script would seem to work if the workbook object were passed to the thread , but the saved excel file would not have data on it. 2)If I try to pass in the worksheet object to the thread the excel file becomes corrupt. Tried all techniques using shared Workbook and Worksheet objects , but had no luck. I had also been through lot of examples and documentation on the Web related to ithreads , thread models and so on , but did not come across anything close to this issue. Would like to know of any feedback the monastery has had with similar attempts made earlier. Here is the script stripped down to the barebone. The original Thought was to gain time at creating a multi tabbed excel file , in which a each tab would be handled by a single thread using an explicit csv file.

#!perl use strict; use warnings; use threads; use threads::shared; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new('test.xls'); my $worksheet = $workbook->add_worksheet('sample'); #---this works processfile($worksheet); #--this will corrupt excel file , irrespective of worksheet being shar +ed or private. #threads->new(\&processfile,$worksheet)->join(); $workbook->close(); sub processfile { my($wrksheet)=@_; $wrksheet->write(0, 0, "Hi Excel!" +); return ; }

Replies are listed 'Best First'.
Re: Excel Worksheets and Perl iThread
by BrowserUk (Patriarch) on Sep 13, 2013 at 19:50 UTC
    The original Thought was to gain time at creating a multi tabbed excel file , in which a each tab would be handled by a single thread using an explicit csv file.

    Sorry to inform you, but that simply will never work.

    You are creating an object in one thread and trying to use (a copy of) that object in another thread. Unless SpreadSheet::WriteExcel and all of its submodules, and all of their dependancies were written to be thread-safe, it would never work.

    They aren't, and it won't.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks. I came across a similar message from one of the links I was reading some time ago. "... every time you start a thread all data structures are copied to the new thread. And when I say all, I mean all. This e.g. includes package stashes, global variables, lexicals in scope. Everything!"

        Thanks. I came across a similar message

        Sorry, but that "message" is neither similar, nor relevant. Ignorance is bliss, and stupid is as stupid does.

        And if you make no attempt to understand the environment or tools you use ...


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.