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

** I think I figured out my problem and fixed it now, thanks! (esp to GrandFather :D )) ** Hi all,

This is what I want to do:
Read a large file and display the results of the file, nicely formatted, in a browser. However, large files can cause browser time outs.

This is what I am doing:

I have created a module with a function that reads in the potentially very large file, and creates an html file displaying the results of the file. Next I do a redirection to the results. My main script calls this module via a thread, and detaches the thread. then the main script keeps refreshing the browser.

The problem:
the detached thread does not work. If I do not detach the thread and make it join before refreshing,or if i call the function directly (non threading), the html file is properly made. However, once i call detach on the thread or not make it join, it does not work.

any ideas??

side question: Is it necessary to detach a thread whenever you don't intend to wait for it to finish before the script ends? Like would it automatically run till the end even tho the main script has finished executing?

Thanks!

Replies are listed 'Best First'.
Re: detached thread not working??
by GrandFather (Saint) on Jul 23, 2007 at 21:10 UTC
      thanks for the reply. .but, I don't understand too much of the code or solutions in that link. :( I am not that good at programming, and I don't quite understand sessions yet.
Re: detached thread not working??
by BrowserUk (Patriarch) on Jul 23, 2007 at 20:50 UTC

    I think you'll need to post the code before we can help you. It's not at all clear (to me at least), what you mean by:

    I have created a module with a function that reads in the potentially very large file, and creates an html file displaying the results of the file. Next I do a redirection to the results. My main script calls this module via a thread, and detaches the thread. then the main script keeps refreshing the browser.

    Particularly the highlighted bit? Words are never a good substitute for the code.

    side question: Is it necessary to detach a thread whenever you don't intend to wait for it to finish before the script ends? Like would it automatically run till the end even tho the main script has finished executing?

    When your main thread terminates, all other threads, regardless of whether they are detached or not, will terminate also. Usually with a message like: "Perl exited with N threads still running".

    Although, the latest incarnation on the threads has (wrongly, IMO) suppressed this message in the case of detached threads. They now die silently!

    Simply put. Your script cannot terminate and leave threads running. When the script dies, the threads die, regardless of their state.

    The fact that you say; "My main script calls this module via a thread, and detaches the thread. then the main script keeps refreshing the browser." suggests to me that you are getting confused between threading and forking, but that would be clarified if you would post the code.


    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.
Re: detached thread not working??
by zer (Deacon) on Jul 23, 2007 at 20:40 UTC
    can you show some of the code?
    I used to come into this problem when i slurped the entire file into a variable. Then spat it out all at once.
    reading line by line with readline is a better method and outputing as you go helps as well. This gives the browser a much quicker response so it can start displaying data earlier.
    this is a work around but may not be the direct answer you are looking for
Re: detached thread not working??
by ursus (Acolyte) on Jul 23, 2007 at 22:34 UTC
    I don't know Perl's threads well enough to solve your problem, I'm afraid. So why am I posting? I noticed piles of print statements in your code. You may want to look at heredoc for better code legibility, particularly the << "EOF" form.
Re: detached thread not working??
by bluegirl123 (Novice) on Jul 23, 2007 at 21:06 UTC
    Ok here is the code for the main script. It is accessed by by another script that passes in a form variable that has its name starting with 'view'. A folder name is also passed in where the files to be read are located.

    I had shortened the file, so if there's useless variables please ignore :-)



    and here is the module code for the actual reading and writing of the html file

    Thanks! Like i said, it works fine if i wait for the thread to finish first..
      Like i said, it works fine if i wait for the thread to finish first...

      I realise that you have an answer that you are happy with. This is purely for my own education, and to satisfy my curiosity. I hope you do not object.

      If your code works when you do not detach it, why are you detching it? That is, what shortcoming are you tryng to address by detaching it?


      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.