Comments on: How to Think About Async Ports
REBOL Technologies

Comments on: How to Think About Async Ports

Carl Sassenrath, CTO
REBOL Technologies

Article #0050
Main page || Index || Prior Article [0049] || Next Article [0051] || 1 Comments || Send feedback

The best way to think about the async port model to consider it similar to the model used for REBOL/View. It is event driven. This approach takes advantage of the fact that most computer programs are waiting most of the time for user interaction or IO to happen (of course there are exceptions, see below).

For example, in /View you write:

window: layout [
    h1 "My Window"
    btn "Button" [print "button1"]
    btn "Quit" [quit]

view window

But, that last line is actually the same as:

view/new window  ; open the window, do not wait
do-events        ; wait for events

DO-EVENTS is simply a call to the WAIT function that will not return until the last window is closed. As window events happen, the view system calls the VID system and events are sent to the interface objects defined above.

This is an event driven method of programming. Like Hypertalk (Apple Hypercard) or Lingo (Macromedia) languages, the "flow" depends on the events that occur. In REBOL you have the choice of writing either "linear" code that executes in sequence like most programming languages or event driven code such as that shown above.

Note that when you write code like the above, it begins in linear mode as you define all the functions and actions of the program. This can be thought of as your setup or initialization phase. During this time, the sequence of execution will not be interrupted. Only when you finally call WAIT (directly or indirectly) the system becomes event driven (asynchronous).

In the new REBOL kernel, the event driven model also works with network ports. So, in addition to your view code, you can easily create network connections during your linear setup code (or actually at any time) and they will also be processed once you call WAIT.

window: layout [
    h1 "My Window"
    btn "Button" [print "button1"]
    btn "Quit" [quit]

view/new window  ; open the window

handler: func [port action arg] [
    ... your handler code ...
server-port: open/direct/binary/async url :handler

... other setup ...


At that point your code becomes event driven and both the GUI and the async ports will be processed.

Here are some important things to note about this mode of operation:

  1. This is not preemptive multitasking (PMT), but it is very similar to cooperative multitasking. View code (feel methods) and port handlers will never be interrupted. You do not need to worry about classical critical sections, semaphores, or mutex most of the time (unless you are trying to coordinate between different ports or faces).
  2. The above note also means that if you do a lot of heavy processing (like image processing), call a synchronous protocol (like the current HTTP, FTP, SMTP, and other protocols), or call a synchronous OS function (like reading a disk file) events will not occur until the action is complete. GUI events and port events will be held off. If this is a big problem for your code you can launch one or more additional copies of REBOL and use those for time consuming operations.
  3. Beware testing async code from the console. The console itself will imply a WAIT while waiting for user input. For example, if you type the above OPEN/async from the console, it will enter the handler with its first event almost immediately. This can become confusing. To avoid this problem, write your test code in a file and use DO to execute it.

Also note that your program may return from the WAIT or DO-EVENTS that is shown above. Errors will cause it to return and closing the main window will cause it to return. A well written program needs to handle those cases. For example, you can deal with errors like this:

... above setup code ...

if error? err: try [do-events][
    show-error disarm :err

... additional cleanup code ...

Here the SHOW-ERROR function may open a new window to report the error to the user. If you want the program to continue, even after the error occurred, you can put a loop around this part. If you do that it is wise to add an error counter for the case where continuing from the error situation is not possible:

loop 3 [
    either error? err: try [do-events][
        show-error disarm :err
        break ; normal exit

Keep in mind that an error can be thrown from a point in your code where state changes (e.g. setting variables, modifying lists) related to your functions has not been concluded. Everything that follows the error point has been left undone.

AltME and other products serve as a good example of this kind of error "capture" and processing. In AltME's case the error is displayed and the user has the option of sending it directly to SafeWorlds bug database.



10-Sep-2011 18:36:15
to what did he smoke choose another nickname ... shadwolf is already taken :P but something shorter then do a group of youngster motivated like you ... and do your best to bring a rebol 1 to 1 clone and you can in that effort for example take one of the 5th clone already existing ... I would be you ... I would choose to bring back to life R# just to slap in the face the people not able to bring it to something worthwhile since then C# in .net framework or in mono progressed heeeeeeeeeeeeelll alot ... damn if only rebol had progressed that much in the same time span ...

Post a Comment:

You can post a comment here. Keep it on-topic.


Blog id:



 Note: HTML tags allowed for: b i u li ol ul font p br pre tt blockquote

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

Updated 25-Nov-2017   -   Copyright Carl Sassenrath   -   WWW.REBOL.COM   -   Edit   -   Blogger Source Code