Tcl Library Source Code
coroutine - Coroutine utilities
Not logged in
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2018 Conference, Houston/TX, US, Oct 15-19
Send your abstracts to
or submit via the online form by Aug 20.

[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]

coroutine(n) 1.1.3 tcllib "Coroutine utilities"


coroutine - Coroutine based event and IO handling


The coroutine package provides coroutine-aware implementations of various event- and channel related commands. It can be in multiple modes:

  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.


All the commands listed below are synchronous with respect to the coroutine invoking them, i.e. this coroutine blocks until the result is available. The overall eventloop is not blocked however.

coroutine::util after delay

This command delays the coroutine invoking it by delay milliseconds.

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.

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.

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.

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 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.

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.

coroutine::util update ?idletasks?

This command causes the coroutine invoking it to run pending events or idle handlers before proceeding.

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

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. Please also report any ideas for enhancements you may have for either package and/or documentation.