Advanced Track: Asynchronous  Threads Bil Corry lasso.pro
What Are Async Threads? Code that executes in parallel to the currently processing page.
Why Use Async Threads? Off-load tasks Improve response time to enduser by off-loading tasks that do not need to provide immediate feedback to the enduser Recurring events Lasso Scheduled Events, Email Queue, etc all are async threads. Network-hooked services lp_site_restart sets up a TCP listener that receives commands to restart Lasso
Typical Lasso Request Model Browser sends HTTP request to the web server
Web server determines the request is for Lasso and passes it to the Lasso connector
Lasso connector communicates with Lasso Server and sends the request
Lasso Server parses request, replies to Lasso connector
Lasso connector replies to web server
Web server replies to browser
Lasso Async Thread Model Lasso page initiates new thread
Just like a normal page, the thread will run until: It terminates normally
It crashes
It is terminated for taking too long
Lasso site or server is shut down When configured as such, some threads never terminate
Lasso Async Thread Model (cont) Because the request comes from Lasso itself, async threads do NOT have access to the web request info, such as client_headers, action_params, etc.
It will also not have access to the variables of the page that created it.
All file paths must be absolute from the hard disk root – [include] does not work
Lasso Async Thread Model (cont) Output from the async thread is not available, so alternative methods must be used, such as logging to a file, a database, global variable or the Lasso console
An async thread will execute according to its priority and the load on Lasso
Basic Example Async threads are surrounded by curly-braces and initiated using ->asasyn c: [{'Hello World'}->asasync] The above code doesn't show anything in the browser, but it does run asynchronously.
Sending Data to Async Thread ->asasync allows you to add params to pass into the async thread [{ var('p1') = params->find('p1')->get(1)->value } ->asasync(-params=array('p1'=1, 'p2'=2))] Can also use globals (or other shared resources), but take care to correctly send the right message to the right thread and use [thread_atomic] to keep access to the global thread-safe
Template For Copying Vars To Async Thread <?LassoScript  // from: http://www.lassotech.com/async // 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 Thread_SetPriority(Thread_GetCurrentID, Thread_Priority_Low); // set priority // recreate vars - 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... ?>

Asynchronous Threads in Lasso 8.5

  • 1.
    Advanced Track: Asynchronous Threads Bil Corry lasso.pro
  • 2.
    What Are AsyncThreads? Code that executes in parallel to the currently processing page.
  • 3.
    Why Use AsyncThreads? Off-load tasks Improve response time to enduser by off-loading tasks that do not need to provide immediate feedback to the enduser Recurring events Lasso Scheduled Events, Email Queue, etc all are async threads. Network-hooked services lp_site_restart sets up a TCP listener that receives commands to restart Lasso
  • 4.
    Typical Lasso RequestModel Browser sends HTTP request to the web server
  • 5.
    Web server determinesthe request is for Lasso and passes it to the Lasso connector
  • 6.
    Lasso connector communicateswith Lasso Server and sends the request
  • 7.
    Lasso Server parsesrequest, replies to Lasso connector
  • 8.
  • 9.
  • 10.
    Lasso Async ThreadModel Lasso page initiates new thread
  • 11.
    Just like anormal page, the thread will run until: It terminates normally
  • 12.
  • 13.
    It is terminatedfor taking too long
  • 14.
    Lasso site orserver is shut down When configured as such, some threads never terminate
  • 15.
    Lasso Async ThreadModel (cont) Because the request comes from Lasso itself, async threads do NOT have access to the web request info, such as client_headers, action_params, etc.
  • 16.
    It will alsonot have access to the variables of the page that created it.
  • 17.
    All file pathsmust be absolute from the hard disk root – [include] does not work
  • 18.
    Lasso Async ThreadModel (cont) Output from the async thread is not available, so alternative methods must be used, such as logging to a file, a database, global variable or the Lasso console
  • 19.
    An async threadwill execute according to its priority and the load on Lasso
  • 20.
    Basic Example Asyncthreads are surrounded by curly-braces and initiated using ->asasyn c: [{'Hello World'}->asasync] The above code doesn't show anything in the browser, but it does run asynchronously.
  • 21.
    Sending Data toAsync Thread ->asasync allows you to add params to pass into the async thread [{ var('p1') = params->find('p1')->get(1)->value } ->asasync(-params=array('p1'=1, 'p2'=2))] Can also use globals (or other shared resources), but take care to correctly send the right message to the right thread and use [thread_atomic] to keep access to the global thread-safe
  • 22.
    Template For CopyingVars To Async Thread <?LassoScript // from: http://www.lassotech.com/async // 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 Thread_SetPriority(Thread_GetCurrentID, Thread_Priority_Low); // set priority // recreate vars - 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... ?>