My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Home  
Updated Jun 25, 2008 by klaasvan...@gmail.com

#A Usage Example

The main example

Friendly Flow allows you to write event driven, multiplexed code which looks like blocking code from a single-client or threaded server.

def foo(sock):
    data = yield sock.readAll(1000)
    #... do interesting stuff with data

    yield sock.writeAll("response")

Both yield statements above yield to a select() based reactor, and return only when

  • for sock.readAll(1000): exactly 1000 bytes have been read from the socket
  • for sock.writeAll("response"): when the entire "response" has been written.

This allows for the small footprint and ease of understanding of a framework like "Twisted" (reactor based), while providing the programmer with a very well known paradigm: a bunch of statements executed top to bottom.

Composition: flatten.py

Composition is also possible. Consider the following normal piece of code

def f():
    return "f return value"

def g():
    return f()

This translates into Friendly Flow code in the following manner:

def f():
    yield RETURN("f return value")

def g()
    fReturn = yield f()

Why the extra RETURN glue? Well, because we use "whatever else we yield" als tokens for the reactor (blocking for read, write and some other options) we need a special token to designate this value needs to go into the value fReturn at g().

Note also that yielding a generator (a method with yield statements) is interpreted as calling it.

Yieldable tokens: scheduler.py

  • RETURN(value) exits the generator and passes the value up to the calling generator (if any)
  • Yielding a generator calls it, and returns the RETURNed value
  • yielding read(fileno) waits for that fileno to become readable (using select).
  • yielding write(fileno) waits for that fileno to become writable (using select)
  • yielding fork(generator) adds a new thread to the reactor

Getting started

  • Check out the svn
  • Look in the example dir for an idea of how to use it, and how to connect the pieces
  • Look at the tests (though they maybe somewhat abstract, they are very precise, and cover at the most important components well)

Credits and resources

  • Erik Groeneveld inspired Klaas to look at Python generators and select() as an Interesting combination. This is called Weightless. see: http://www.weightless.io
  • Multitask does something very similar to Friendly Flow. Unfortunately I found out about its existence only when Friendly Flow had implemented pretty much the same features... http://o2s.csail.mit.edu/o2s-wiki/multitask

Contact

There's no lists yet. Friendly Flow is currently developed by Klaas van Schelven, Xaba Software. Xaba Software is here: www.xaba.nl (Dutch). Mail Klaas: klaas@vanschelven.com


Sign in to add a comment
Powered by Google Project Hosting