# read() subtlety

Gustavo Glusman (bmgustav@bioinformatics.weizmann.ac.il)
Thu, 12 Sep 1996 00:00:41 PDT

- Messages sorted by:[ date ][ thread ][ subject ][ author ]

    Consider this bit of code:

     7:  while (this:check_status(which))

     8:    this.tasks[which] = task_id();

     9:    what = read(whom);

    10:    if (typeof(what) == STR)

    11:      this:handle(what, whom);

    12:    endif

    13:  endwhile

    'whom' is an active connection, 'which' is some index associated with
    it.

    To my surprise, this generates (infrequently) a traceback of 'out of
    ticks' ... on line 10.

    Now, since read() here is called without a true 2nd parameter, it
    should be a 'blocking' read, which should suspend the task. Therefore,
    line 10 should be reached with the maximum possible ticks and seconds
    for a background task.

    The manual says:

       * The `read()' function also suspends the execution of the current
    task, in

         this case waiting for the player to type a line of input.  When
    the line

         is received, the task resumes with the `read()' function returning
    the

         input line as result.  These are called "reading tasks".

    And also:

     - Function: str read ([obj CONN [, NON-BLOCKING]])

         Reads and returns a line of input from the connection CONN (or, if
    not

         provided, from the player that typed the command that initiated
    the

         current task).  If NON-BLOCKING is false or not provided, this
    function

         suspends the current task, resuming it when there is input
    available to

         be read.  If NON-BLOCKING is provided and true, this function
    never

         suspends the calling task; if there is no input currently
    available for

         input, `read()' simply returns 0 immediately.

    This seems to imply that read() *always* suspends, even if input is
    already available thorugh that connection. But the behaviour seems to
    suggest that some of the read() calls are not suspending? How else
    could it run out of ticks on line 10?

    -------------------------------------------------------------

    Gustavo Glusman               Founder/administrator of BioMOO

    -- Gustavo@bioinformatics.weizmann.ac.il

    -- http://bioinformatics.weizmann.ac.il/Gustavo

    -- BioMOO: telnet bioinformatics.weizmann.ac.il 8888

               WWW:   http://bioinfo.weizmann.ac.il/BioMOO