spawn
To perform several WebDNA actions simultaneously, place them 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.
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, flush a database to disk 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:
- MIME headers from the browser.
- Cookies from the browser.
- Form variables (or URL parameters from HREF hyperlink).
- Math variables created in this template.
- Text variables created in this template.
Example WebDNA code:
Before the spawn [elapsedtime]
[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]
The example above yields:
Before the spawn 1
After the spawn 3
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 | |
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. | |
Incorrect | Correct |
|
|
Why | |
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. |
Nothing within a SPAWN context will be delivered to the browser, nor will any HTML within the SPAWN be interpreted. SPAWN is intended to run time-consuming server-side tasks (like large database updates) without making the browser wait for it to complete.
Example WebDNA code:
The following code send one email every 30 seconds. If you're only sending 500 emails occasionally this will send them in less than 5 hours.
[spawn]
[search db=xxx.db&neemaildatarq=[blank]]
[founditems]
[text]wffSuccess=no[/text]
[waitforfile thisFileWillNeverExist.txt][/waitforfile]
[showif [wffSuccess]=no]
[sendmail from=xxx@xxx.xxx&to=[email]&subject=not spam]
Blah blah blah ...
[/sendmail]
[text]wffSuccess=yes[/text]
[/showif]
[/founditems]
[/search]
[/spawn]