:TITLE: Child interpreters ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl43.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: The interp command creates new child interpreters within an existing interpreter. The child interpreters can have their own sets of variables and files, or they can be given access to items in the parent interpreter.
If the child is created with the -safe option, it will not be able to access the file system, or otherwise damage your system. This feature allows a script to evaluate code from an unknown (and untrusted) site.
The names of child interpreters are a hierarchical list. If interpreter
foo is a child of interpreter bar, then it can be
accessed from the toplevel interpreter as {bar foo}.
The primary interpreter (what you get when you type tclsh) is the empty list {}.
The interp command has several subcommands and options. A critical subset is:
Note that slave interpreters have a separate state and namespace, but do not have separate event loops. These are not threads, and they will not execute independently. If one slave interpreter gets stopped by a blocking I/O request, for instance, no other interpreters will be prcessed until it has unblocked. :TEXT_END: :LESSON_TEXT_START_LEVEL 1: For most applications, a single interpreter and subroutines are quite sufficient. However, if you are building a client-server system (for example) you may need to have several interpreters talking to different clients, and maintaining their state. You can do this with state variables, naming conventions, or swapping state to and from disk, but that gets messy.
The interp command creates new child interpreters within an existing interpreter. The child interpreters can have their own sets of variables and files, or they can be given access to items in the parent interpreter.
If the child is created with the -safe option, it will not be able to access the file system, or otherwise damage your system. This feature allows a script to evaluate code from an unknown (and untrusted) site.
The names of child interpreters are a hierarchical list. If interpreter
foo is a child of interpreter bar, then it can be
accessed from the toplevel interpreter as {bar foo}.
The primary interpreter (what you get when you type tclsh) is the empty list {}.
The interp command has several subcommands and options. A critical subset is:
Note that slave interpreters have a separate state and namespace, but do not have separate event loops. These are not threads, and they will not execute independently. If one slave interpreter gets stopped by a blocking I/O request, for instance, no other interpreters will be prcessed until it has unblocked.
The example below shows two child interpreters being created under
the primary interpreter {}. Each of these interpreters is given a variable
name which contains the name of the interpreter.
Note that the alias command causes the procedure to be evaluated in the
interpreter in which the procedure was defined, not the interpreter
in which it was evaluated. If you need a procedure to exist within
an interpreter, you must interp eval a proc
command within that interpreter. If you want an interpreter to be
able to call back to the primary interpreter (or other interpreter)
you can use the interp alias command.
:TEXT_END:
:LESSON_TEXT_START_LEVEL 2:
For most applications, a single interpreter and subroutines are
quite sufficient. However, if you are building a client-server
system (for example) you may need to have several interpreters
talking to different clients, and maintaining their state. You
can do this with state variables, naming conventions, or swapping
state to and from disk, but that gets messy.
The interp command creates new child interpreters within an existing interpreter. The child interpreters can have their own sets of variables and files, or they can be given access to items in the parent interpreter.
If the child is created with the -safe option, it will not be able to access the file system, or otherwise damage your system. This feature allows a script to evaluate code from an unknown (and untrusted) site.
The -safe option allows you to build client/server based systems in which a Client sends Tcl commands to the Server, and the server can evaluate these commands in a safe interpreter to ensure that rogue clients do not damage the Server. This gives you a great deal of power, since you don't need to define any new syntax for the client/server communication, or write any special parsing code.
The names of child interpreters are a hierarchical list. If interpreter
foo is a child of interpreter bar, then it can be
accessed from the toplevel interpreter as {bar foo}.
The primary interpreter (what you get when you type tclsh) is the empty list {}.
The interp command has several subcommands and options. A critical subset is:
Note that slave interpreters have a separate state and namespace, but do not have separate event loops. These are not threads, and they will not execute independently. If one slave interpreter gets stopped by a blocking I/O request, for instance, no other interpreters will be prcessed until it has unblocked.
The example below shows two child interpreters being created under
the primary interpreter {}. Each of these interpreters is given a variable
name which contains the name of the interpreter using the
interp eval $int [list set name $int] command.
Note the uses of list, quotes and bracketed strings for the
interp eval commands. The interp eval command
concatenates the arguments into a list, as the eval command
does. If you try to interp eval a command like :
interp eval $foo [list puts "the value of $bar in $foo is"]
Both the $foo and $bar will be substituted in
the parent interpreter before the line is evaluated in the child interpreter.
If you escape the $bar to substitute $foo in the parent
and $bar in the child like this:
interp eval $foo [list puts "the value of bar in $foo is \$bar"]
You'll find that the output resembles :
the value of bar in interp2 is $bar
Using the list to group the arguments to interp eval
made the command passed to the child interpreter resemble this:
puts {the value of bar in interp2 is $bar}
Because lists are grouped with braces. Since the argument to puts
in the child interpreter is grouped with braces, no substitution occurs.
If you want substitution to occur within the child interpreter, you may need to create the command using braces, or using the format or concat commands as shown in lessons 32, 33 and 34.
The last line in the example is an error.
The interp alias command causes the procedure to be evaluated in the
interpreter in which the procedure was defined, not the interpreter
in which it was evaluated. If you need a procedure to exist within
an interpreter, you must interp eval a proc
command within that interpreter. If you want an interpreter to be
able to call back to the primary interpreter (or other interpreter)
you can use the interp alias command.
:TEXT_END:
:CODE_START:
set i1 [interp create firstChild]
set i2 [interp create secondChild]
puts "first child interp: $i1"
puts "second child interp: $i2\n"
# Set a variable "name" in each child interp, and
# create a procedure within each interp
# to return that value
foreach int [list $i1 $i2] {
interp eval $int [list set name $int]
interp eval $int {proc nameis {} {global name; return "nameis: $name";} }
}
foreach int [list $i1 $i2] {
interp eval $int "puts \"EVAL IN $int: name is \$name\""
puts "Return from 'nameis' is: [interp eval $int nameis]"
}
#
# A short program to return the value of "name"
#
proc rtnName {} {
global name
return "rtnName is: $name"
}
#
# Alias that procedure to a proc in $i1
interp alias $i1 rtnName {} rtnName
puts ""
# This is an error. The alias causes the evaluation
# to happen in the {} interpreter, where name is
# not defined.
puts "firstChild reports [interp eval $i1 rtnName]"
:TEXT_END: