Technical References - [spawn]

Creates a new thread to execute WebDNA simultaneously with the current template.

numero = 243
interpreted = N
texte = [spawn]Some Complex WebDNA[/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:
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:

IncorrectCorrect
[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]
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.
IncorrectCorrect
[search blah][founditems][spawn][FieldFromDB][/spawn][/founditems][/search]
[search blah&max=10][founditems][Text]value=[FieldFromDB][/Text][spawn][value][/spawn][/founditems][/search]
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 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]
[spawn]Some Complex WebDNA[/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:

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:


IncorrectCorrect
[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]
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.
IncorrectCorrect
[search blah]
[founditems]
[spawn]
[FieldFromDB]
[/spawn]
[/founditems]
[/search]
[search blah&max=10]
[founditems]
[text]value=[FieldFromDB][/Text]
[spawn]
[value]
[/spawn]
[/founditems]
[/search]
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
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]


DOWNLOAD WEBDNA NOW!

Top Articles:

AWS Raw WebDNA LAMP-Plus WebServer

Amazon Web Services (AWS) README for Machine Image ID...

WebDNA Modules

A list of the currently available modules...

Tips and Tricks

A list of user-submitted tips ...

F.A.Q

A compilation of some user's questions...

WebDNA Libraries

A list of available libraries for WebDNA...

Technical Change History

This Technical Change History provides a reverse chronological list of WebDNA changes...

Related Readings:

[returnraw]

Sends 'raw' MIME headers and data back to browser...

[cookie]

Cookies are a great way to remember visitors...

[lookup]

...

[tcpconnect]

A powerful feature to connect to a TCP port of another computer on the Internet...

[username]

Using [password] and [username] is an easy was to show the browser login dialog box...

[wait]

[wait] just does what it says...