coroutine(3tcl)
πΈ coroutine β Coroutine utilities π
ok so bestie, this is the manual for coroutine(3tcl) from tcllib, and honestly? it's giving green threads but make it cute. sit down, grab a boba, we're doing this. β¨
β¨ NAME β¨
coroutine β coroutine based event and IO handling. that's it, that's the name, she's simple and she knows it π«Ά
π SYNOPSIS π
package require Tcl 8.6 9
package require coroutine 1.4
coroutine::util after delay
coroutine::util await varname...
coroutine::util create arg...
coroutine::util exit ?status?
coroutine::util gets chan ?varname?
coroutine::util gets_safety chan limit varname
coroutine::util global varname...
coroutine::util puts ?-nonewline? channel string
coroutine::util read -nonewline chan ?n?
coroutine::util socket ?options...? host port
coroutine::util update ?idletasks?
coroutine::util vwait varname
π DESCRIPTION π
the coroutine package hands you coroutine-aware versions of a whole bunch of event- and channel-related commands. like, the regular ones but they actually know what a coroutine is. iconic behaviour honestly.
and you can run it in multiple modes, pick your fighter:
- call the commands through their ensemble, in code that was explicitly written to live inside coroutines. you know, the code that knows what it is. main character energy π
- import the commands into a namespace β either directly or through
namespace path. this lets you use them from code that is not coroutine-aware per se and is restricted to specific namespaces. sneaky but legal. π§
and there's an even more aggressive version of this whole "make your code forget coroutines exist" thing than point 2 up there β it's available through the package coroutine::auto, which intercepts the relevant builtin commands and swaps out their implementation depending on the context they're run in. i.e. whether they're inside or outside a coroutine. she just knows. π₯
π API π
every single command listed below is synchronous with respect to the coroutine invoking it, i.e. that coroutine blocks until the result is available. but bestie, the overall eventloop is not blocked. the world keeps spinning, only you are waiting. no cap. β¨
coroutine::util after delayβ this command delays the coroutine invoking it bydelaymilliseconds. that's it. it's a nap. take the nap. π΄coroutine::util await varname...β this one is an extension form of thecoroutine::util vwaitcommand (scroll down, she's there π). it waits on a write to one of many named namespace variables. plural. the whole squad.coroutine::util create arg...β creates a new coroutine with an automatically assigned name and causes it to run the code specified by the arguments. surprise baby, no name needed. π¦coroutine::util exit ?status?β exits the current coroutine, causing it to returnstatus. if no status was specified, the default0is returned. bye bestie, exit with dignity πcoroutine::util gets chan ?varname?β reads a line from the channelchanand returns it either as its result, or β if avarnamewas specified β writes it to the named variable and returns the number of characters read. two ways to get your tea βcoroutine::util gets_safety chan limit varnameβ reads a line from the channelchanup to sizelimitand stores the result invarname. if thelimitis reached before the first newline shows up, an error is thrown. it's the safety rails version, she's cautious and we love her for it π the command returns the number of characters read.coroutine::util global varname...β imports the named global variables of the coroutine into the current scope. technically speaking, these variables reside in level#1of the Tcl stack. so no, these are not the regular globals in the global namespace β each coroutine can have its own set, completely independent of all the others. your own lil private stash πcoroutine::util puts ?-nonewline? channel stringβ writes the string to the specified channel. and contrary to the builtinputs, this one waits until the channel is actually writable before writing to it. patient queen πcoroutine::util read -nonewline chan ?n?β readsncharacters from the channelchanand returns them as its result. ifnisn't specified, the command reads the channel until EOF is reached. the whole thing. all of it. πcoroutine::util socket ?options...? host portβ connects to the specifiedhostandportand returns when that's done. contrary to the builtin command, it performs a non-blocking connect in the background. so yeah, it blocks the calling coroutine, but the overall application is not blocked. everybody else keeps vibing βcoroutine::util update ?idletasks?β causes the coroutine invoking it to run pending events or idle handlers before proceeding. like checking your notifs before you continue the convo π±coroutine::util vwait varnameβ causes the coroutine calling it to wait for a write to the named namespace variablevarname. one variable, one wait, lots of patience πΈ
π BUGS, IDEAS, FEEDBACK π
this document, and the package it describes, will undoubtedly contain bugs and other problems. we're being honest upfront, that's growth. π
please report such in the category coroutine of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. please also report any ideas for enhancements you may have for either the package and/or the documentation. we want the ideas, we want all of them π«Ά
when proposing code changes, please provide unified diffs, i.e. the output of diff -u.
note further that attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. ok that's very specific but like, do it that way fr fr.
β KEYWORDS β
after, channel, coroutine, events, exit, gets, global, green threads, read, threads, update, vwait
π CATEGORY π
Coroutine
π COPYRIGHT π
Copyright (c) 2010-2015 Andreas Kupries <andreas_kupries@users.sourceforge.net>
and that's the whole page bestie, you ate it π
this was tcllib version 1.4, the coroutine(3tcl) manual, and we are so done here. go write some coroutines, you're gonna slay. periodt. πβ¨π
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2010-2015 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms