Lasso Soft Inc. > Home

  • Articles

Spawning Asynchronous Threads

Spawn an asynchronous thread complete with the current page vars.

Problem

You want to return the page quickly to the user, while allowing Lasso to continue processing in a separate thread.

Solution

Below is a generic template that you would add the the page you want to split into one or more threads. It will copy all vars currently defined on the page to the async thread.

<?LassoScript

// stuff on your page not async here...

// --------------  BEGIN OF ASYNC CODE --------------
{
// --------------  BEGIN OF ASYNC CODE --------------

    // --------------------
    //   process async!
    // --------------------
    //
    // NOTE: Code below this point is processed in it's own thread!
    //

    Lasso_ExecutionTimeLimit(60 * 60); // in seconds, set this to something reasonable, currently set to 1 hour
    Thread_SetPriority(Thread_GetCurrentID, Thread_Priority_Low); // set priority to low, give more priority to normal web requests (can also make it high if needed)

    // recreate vars - don't change the code below - this makes all the vars on the page available to the async thread
    iterate: params->(find:'-vars')->(get:1)->value, local:'var';
        (var: #var->name) = #var->value;
    /iterate;

    // do stuff here that you want in it's own thread...

// --------------  END OF ASYNC CODE --------------
}->(asasync: -params=(array: '-vars' = vars));
// --------------  END OF ASYNC CODE -------------- 

// do more stuff on the current page...

?>

Things to note

  • The async thread will not return any data to the currently executing page. This make it very difficult to debug your code within the async thread. One solution is to output to Lasso's error system, using [log_critical].
  • Within the async thread, all file paths must be absolute from the hard disk root.

So for example, if you had this before:

  include('myfile.inc')

You'd have to change it to this within the async thread:

process(file_read('///path/from/disk/root/myfile.inc'));

 

Be sure that the async thread has the proper file permissions.

Author: Bil Corry
Created: 30 Dec 2008
Last Modified: 16 Mar 2011

Comments

No comments found
You must be logged in to comment.

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft