coroutine(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

✨ coroutine(n) β€” the Coroutine utilities glow-up ✨

okay bestie, gather round πŸ’– this is the page for coroutine, from the tcllib manual, version 1.4. It's all about coroutine-based event and IO handling, and honestly? it's giving main character energy fr fr.


🌸 NAME 🌸

coroutine β€” Coroutine based event and IO handling πŸ¦‹


✨ 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 πŸ’…

So the coroutine package gives you coroutine-aware versions of a bunch of event- and channel-related commands. No cap. And it can live in multiple modes, pick your vibe:

  1. Call the commands through their ensemble, in code which is explicitly written for use within coroutines. πŸŽ€
  2. Import the commands into a namespace, either directly, or through namespace path. This allows the use from within code which is not coroutine-aware per se and restricted to specific namespaces. 🧚

A more agressive form of making code coroutine-oblivious than point 2 above is available through the package coroutine::auto, which intercepts the relevant builtin commands and changes their implementation dependending on the context they are run in, i.e. inside or outside of a coroutine. (yes bestie, "agressive" and "dependending" are spelled exactly like that in the source, we don't fix, we respect πŸ’€)


πŸ‘‘ API πŸ‘‘

Real talk first: all the commands listed below are synchronous with respect to the coroutine invoking them, i.e. this coroutine blocks until the result is available. But β€” and this is the iconic part β€” the overall eventloop is not blocked. Periodt. πŸ’…

  • coroutine::util after delay β€” This command delays the coroutine invoking it by delay milliseconds. Little nap. Self-care. 😴
  • coroutine::util await varname... β€” This command is an extension form of the coroutine::util vwait command (see below) which waits on a write to one of many named namespace variables. Not just one variable, bestie, many. Obsessed. πŸ’–
  • coroutine::util create arg... β€” This command creates a new coroutine with an automatically assigned name and causes it to run the code specified by the arguments. New coroutine just dropped, name included, no input needed from you. ✨
  • coroutine::util exit ?status? β€” This command exits the current coroutine, causing it to return status. If no status was specified the default 0 is returned. Bye babe, here's your exit code 🫢
  • coroutine::util gets chan ?varname? β€” This command reads a line from the channel chan and returns it either as its result, or, if a varname was specified, writes it to the named variable and returns the number of characters read.
  • coroutine::util gets_safety chan limit varname β€” This command reads a line from the channel chan up to size limit and stores the result in varname. Of limit is reached before the set first newline, an error is thrown. The command returns the number of characters read. (yes, "Of" β€” that's the original text, we're keeping it fr)
  • coroutine::util global varname... β€” This command imports the named global variables of the coroutine into the current scope. From the technical point of view these variables reside in level #1 of the Tcl stack. I.e. these are not the regular global variable in to the global namespace, and each coroutine can have their own set, independent of all others. So like... coroutine-global, not global-global. Important distinction, bestie πŸ’€
  • coroutine::util puts ?-nonewline? channel string β€” This commands writes the string to the specified channel. Contrary to the builtin puts this command waits until the channel is writable before actually writing to it. Patient king behavior πŸ‘‘
  • coroutine::util read -nonewline chan ?n? β€” This command reads n characters from the channel chan and returns them as its result. If n is not specified the command will read the channel until EOF is reached. (-nonewline is right there in the form, no default noted, just how it is 🌸)
  • coroutine::util socket ?options...? host port β€” This command connects to the specified host and port and returns when that is done. Contrary to the builtin command it performs a non-blocking connect in the background. As such, while its blocks the calling coroutine, the overall application is not blocked. (again: "its blocks" β€” original wording, untouched 😭)
  • coroutine::util update ?idletasks? β€” This command causes the coroutine invoking it to run pending events or idle handlers before proceeding. Quick lil processing moment, then we move. 🌈
  • coroutine::util vwait varname β€” This command causes the coroutine calling it to wait for a write to the named namespace variable varname.

πŸ™ˆ BUGS, IDEAS, FEEDBACK πŸ™ˆ

ngl, this document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category coroutine of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist](http://core.tcl.tk/tcllib/reportlist). Please also report any ideas for enhancements you may have for either package and/or documentation. We love a feedback moment πŸ’–

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. That's the whole recipe bestie, no shortcuts πŸŽ€


πŸ”₯ KEYWORDS πŸ”₯

after, channel, coroutine, events, exit, gets, global, green threads, read, threads, update, vwait


🌟 CATEGORY 🌟

Coroutine


Copyright (c) 2010-2015 Andreas Kupries <andreas_kupries@users.sourceforge.net>


okay that's the whole page, you ate that πŸ’… this was coroutine(n), tcllib version 1.4, and it's been iconic since forever. go write some coroutines bestie, i believe in you πŸ¦‹βœ¨πŸ’–

πŸ“¦ where this page comes from