Spawn Context [Spawn]Some Complex WebDNA[/Spawn]
Creates a new thread to execute WebDNA simultaneously with the current template.

To perform WebDNA simultaneously, place it inside a Spawn context. All WebDNA inside the Spawn context begins to execute immediately, and the remainder of the template is returned to the visiting browser immediately.

Note: The HTML output from within a Spawn context is never displayed to the browser. While this may seem unhelpful at first, realize that the purpose of Spawn is to allow you to execute very lengthy operations without forcing the visitor to wait for them. The WebDNA in the spawned context could update a database several minutes later, wait for a 15 second credit card operation, create a WebDelivery file, or many other useful things.

Not all WebDNA information is available inside a [Spawn] context, because often the outer template that created the spawn is already gone from memory, and thus its context information is gone as well. Only the following values are available inside the spawn, because a copy of them is made before creating the spawn context:

  1. MIME headers from the browser.
  2. Cookies from the browser.
  3. Form variables (or URL parameters from HREF hyperlink).
  4. Math variables created in this template.
  5. Text variables created in this template.

Example (normally you would put the following text into a .tpl file on your server and use a web browser to link to it):

Before the spawn [elapsedtime]<br>
[Spawn]
-- Some WebDNA that takes a long time to finish
[Loop start=1&end=5000][ShowIf 1=1][/ShowIf][/Loop]
[/Spawn]
After the spawn [elapsedtime]<br>

The example above yields:
Before the spawn 1
After the spawn 3

Notice that the elapsedtime is very small, even though the loop inside the spawn could take several seconds. This is because your web browser sees the results of the template before the spawned WebDNA is finished.

Here are some common mistakes you should avoid:

Incorrect Correct Why
[Loop start=1&end=10]
  [Spawn]
  [ShowIf [index]=5]
    stuff
  [/ShowIf]
  [/Spawn]
[/Loop]
[Loop start=1&end=10]
  [Math]LoopIndex=[index][/Math]
  [Spawn]
  [ShowIf [LoopIndex]=5]
    stuff
  [/ShowIf]
  [/Spawn]
[/Loop]
Remember Spawn might start executing long after the original template that was created has gone away. Spawn has no idea what the value of [index] is, because that comes from the outer [Loop] context, which really 'belongs' to the now-gone exterior template. The correct method is to create a math variable to hold the [index] value, because Spawn does keep a copy of all the math variables in existence when it was created.
[Search blah]
  [FoundItems]
    [Spawn]
      [FieldFromDB]
    [/Spawn]
  [/FoundItems]
[/Search]
[Search blah&max=10]
  [FoundItems]
    [Text]value=[FieldFromDB][/Text]
    [Spawn]
      [value]
    [/Spawn]
  [/FoundItems]
[/Search]
This is bad for two reasons. Similar to the first example, Spawn has no idea what the database field values are, because it is not truly inside the [FoundItems] context. Second, be very careful you do no create too many spawns -- they can use a lot of memory, and in this case if the [FoundItems] is more than a dozen or so; web server performance can degrade considerably. The correct example limits the number of spawns, and also uses a text variable to hold the value of the database field.

User Contributed Notes
SpawnContext  

Copyright © 2008 WebDNA Software Corporation, Inc.