coroutine - Online Linux Manual PageSection : n
Updated : 8.6
Source : Tcl
Note : Tcl Built-In Commands
NAMEcoroutine, yield, yieldto − Create and produce values from coroutines
SYNOPSIScoroutine name command ?arg...?
yield ?value?
yieldto command ?arg...?
name ?value...?
\{.ev 2
.\} .fi
.\}
DESCRIPTIONThe coroutine command creates a new coroutine context (with associated command) named name and executes that context by calling command, passing in the other remaining arguments without further interpretation. Once command returns normally or with an exception (e.g., an error) the coroutine context name is deleted. Within the context, values may be generated as results by using the yield command; if no value is supplied, the empty string is used. When that is called, the context will suspend execution and the coroutine command will return the argument to yield. The execution of the context can then be resumed by calling the context command, optionally passing in the single value to use as the result of the yield call that caused the context to be suspended. If the coroutine context never yields and instead returns conventionally, the result of the coroutine command will be the result of the evaluation of the context. The coroutine may also suspend its execution by use of the yieldto command, which instead of returning, cedes execution to some command called command (resolved in the context of the coroutine) and to which any number of arguments may be passed. Since every coroutine has a context command, yieldto can be used to transfer control directly from one coroutine to another (this is only advisable if the two coroutines are expecting this to happen) but any command may be the target. If a coroutine is suspended by this mechanism, the coroutine processing can be resumed by calling the context command optionally passing in an arbitrary number of arguments. The return value of the yieldto call will be the list of arguments passed to the context command; it is up to the caller to decide what to do with those values. The recommended way of writing a version of yield that allows resumption with multiple arguments is by using yieldto and the return command, like this: proc yieldm {value} {
yieldto return -level 0 $value
}
\{.ev 2
.\} The coroutine can also be deleted by destroying the command name, and the name of the current coroutine can be retrieved by using info coroutine. If there are deletion traces on variables in the coroutine's implementation, they will fire at the point when the coroutine is explicitly deleted (or, naturally, if the command returns conventionally). At the point when command is called, the current namespace will be the global namespace and there will be no stack frames above it (in the sense of upvar and uplevel). However, which command to call will be determined in the namespace that the coroutine command was called from.
EXAMPLESThis example shows a coroutine that will produce an infinite sequence of even values, and a loop that consumes the first ten of them. proc allNumbers {} {
yield
set i 0
while 1 {
yield $i
incr i 2
}
}
coroutine nextNumber allNumbers
for {set i 0} {$i < 10} {incr i} {
puts "received [nextNumber]"
}
rename nextNumber {}
In this example, the coroutine acts to add up the arguments passed to it. coroutine accumulator apply {{} {
set x 0
while 1 {
incr x [yield $x]
}
}}
for {set i 0} {$i < 10} {incr i} {
puts "$i -> [accumulator $i]"
}
This example demonstrates the use of coroutines to implement the classic Sieve of Eratosthenes algorithm for finding prime numbers. Note the creation of coroutines inside a coroutine. proc filterByFactor {source n} {
yield [info coroutine]
while 1 {
set x [$source]
if {$x % $n} {
yield $x
}
}
}
coroutine allNumbers apply {{} {while 1 {yield [incr x]}}}
coroutine eratosthenes apply {c {
yield
while 1 {
set n [$c]
yield $n
set c [coroutine prime$n filterByFactor $c $n]
}
}} allNumbers
for {set i 1} {$i <= 20} {incr i} {
puts "prime#$i = [eratosthenes]"
}
This example shows how a value can be passed around a group of three coroutines that yield to each other: proc juggler {name target {value ""}} {
if {$value eq ""} {
set value [yield [info coroutine]]
}
while {$value ne ""} {
puts "$name : $value"
set value [string range $value 0 end-1]
lassign [yieldto $target $value] value
}
}
coroutine j1 juggler Larry [
coroutine j2 juggler Curly [
coroutine j3 juggler Moe j1]] "Nyuck!Nyuck!Nyuck!"
\{.ev 2
.\}
DETAILED SEMANTICSThis example demonstrates that coroutines start from the global namespace, and that command resolution happens before the coroutine stack is created. proc report {where level} {
# Where was the caller called from?
set ns [uplevel 2 {namespace current}]
yield "made $where $level context=$ns name=[info coroutine]"
}
proc example {} {
report outer [info level]
}
namespace eval demo {
proc example {} {
report inner [info level]
}
proc makeExample {} {
puts "making from [info level]"
puts [coroutine coroEg example]
}
makeExample
}
Which produces the output below. In particular, we can see that stack manipulation has occurred (comparing the levels from the first and second line) and that the parent level in the coroutine is the global namespace. We can also see that coroutine names are local to the current namespace if not qualified, and that coroutines may yield at depth (e.g., in called procedures). making from 2
made inner 1 context=:: name=::demo::coroEg
SEE ALSOapply(n), info(n), proc(n), return(n)
KEYWORDScoroutine, generator 0
Johanes Gumabo
Data Size : 24,045 byte
man-yieldto.nBuild : 2024-12-05, 20:55 :
Visitor Screen : x
Visitor Counter ( page / site ) : 6 / 171,259
Visitor ID : :
Visitor IP : 3.15.10.139 :
Visitor Provider : AMAZON-02 :
Provider Position ( lat x lon ) : 39.962500 x -83.006100 : x
Provider Accuracy Radius ( km ) : 1000 :
Provider City : Columbus :
Provider Province : Ohio , : ,
Provider Country : United States :
Provider Continent : North America :
Visitor Recorder : Version :
Visitor Recorder : Library :
Online Linux Manual Page : Version : Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.05
Online Linux Manual Page : Library : lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page : Library : lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base : Version : Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base : Library : lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36
Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.
ERROR : Need New Coding : (rof_escape_sequence_block|26|(null)|(null)|-1|139) (rof_escape_sequence|91|yieldto.n|286/157/286/158|\{.ev 2|\{.ev 2
) (rof_escape_sequence|91|yieldto.n|286/166|\} |.\}
) (parse_manual_page_|249|yieldto.n|286/166|\}|.\}
) (rof_fi|19|yieldto.n|287|1|.fi
) (parse_manual_page_|249|yieldto.n|288/139|══─}─══|.══─}─══
) (htmlprn|149|yieldto.n|288/139|.══─}─══|.══─}─══
) (rof_escape_sequence|91|yieldto.n|288/140|\}|.\}
) (parse_manual_page_|249|yieldto.n|288/140|\}|.\}
) (rof_escape_sequence|91|yieldto.n|331/157/331/158|\{.ev 2 |\{.ev 2
) (rof_escape_sequence|91|yieldto.n|331/166|\} |.\}
) (parse_manual_page_|249|yieldto.n|331/166|\}|.\}
) (rof_escape_sequence|91|yieldto.n|426/157/426/158|\{.ev 2 |\{.ev 2
) (rof_escape_sequence|91|yieldto.n|426/166|\} |.\}
) (parse_manual_page_|249|yieldto.n|426/166|\}|.\}
)