You are mixing two things: callback and asynchronous processing (in the sense of multi tasking), which work well together but are not bundled. That's why you are confused.

First of all callbacks should be non-blocking to allow multi tasking and your sleep is blocking by definition.

Think of callbacks as delayed execution, which are triggered by an event instead of executing things in a linear way.

(like adding tasks to a To-do list, like post-its on your fridge)

But it's the event management outside of the callback which allows multitasking.

The event management is deciding how to prioritize and bundle tasks on a To-do list and checking for urgent things from the Inbox...

(A call from hospital might be more important than repairing the lawnmower...)

Pushing a button in a GUI is such an event, but to be able to react the main program must be waiting for such an event instead of being blocked by longer execution or a sleep.

(you might miss telephone while working too long in the garden)

And while it's waiting it can call other callbacks from a queue which need to guaranty to run so fast that reacting to an event isn't hindered.

That's roughly the model of collaborative multitasking.

In a non-collaborative model sleep is implemented in a way to freeze a callback (read thread) and return control to the event manager who can continue the frozen thread later.

(Ideally a thread can be frozen nearly instantly after short running atomar steps, like finishing a page in a book and putting a bookmark)

But Perl isn't optimized for the latter and would need modules like AnyEvent to do so

HTH!

updates

1. callbacks are not necessarily non-blocking, they can simply be a comfortable way to define actions like in grep or File::Find . No real relation to multi threading here.

2. you mentioned fork, thats the non-collaborative way to allow multi-tasking on the OS level. Beside overhead (the whole perl process might be copied) it also involves the need of inter process communication.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!


In reply to Re: Understanding callbacks (non-blocking and multi tasking) by LanX
in thread Understanding callbacks by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.