Monday, November 19, 2007

TCP Networked Tasklets in Python

Fibra 3 introduces some networking features.

Generator based tasklets can now communicate with each other over TCP! The networking plugin uses Twisted to do its magic. What other sorts of plugins might be useful? I'm running out of ideas now. :-)

This is the code for a simple server which echoes everything it receives, and starts the conversation with a 'hi.' message:
import fibra
import fibra.plugins.sleep
import fibra.plugins.tasks as tasks
import fibra.plugins.network as network


def listener():
conn = (yield network.ListenForNewConnection(1980))
conn.send_string('hi.')
yield echo(conn)
yield watch(conn)

def watch(connection):
yield network.WaitForLostConnection(connection)
print connection, ' has been lost'

def echo(connection):
while True:
data = (yield network.WaitForData(connection))
connection.send_string(data)

s = fibra.Schedule()
s.register_plugin(tasks.TaskPlugin())
s.register_plugin(network.NetworkPlugin())

s.install(listener())

while s.tick(): pass

This is the code for a simple client which echoes everything it receives:
import fibra
import fibra.plugins.sleep
import fibra.plugins.tasks as tasks
import fibra.plugins.network as network


def watch(connection):
yield network.WaitForLostConnection(connection)
print connection, ' has been lost'

def echo(connection):
while True:
data = (yield network.WaitForData(connection))
connection.send_string(data)

def connector():
conn = (yield network.ConnectToHost(('localhost',1980)))
yield echo(conn)
yield watch(conn)

s = fibra.Schedule()
s.register_plugin(tasks.TaskPlugin())
s.register_plugin(network.NetworkPlugin())

s.install(connector())

while s.tick(): pass

7 comments:

Anonymous said...

Cool stuff!

Richard Jones said...

When trying to install it via "easy_install fibra" I got:

File "setup.py", line 1, in module
ImportError: No module named ez_setup

:(

I'd be interested to see a side-by-size comparison with a pure Twisted version.

Simon Wittber said...

Damn. I should have tested that. All fixed now.

Richard Jones said...

It's not entirely clear what's going on in that code. When I add a debug "print" to the client echo function so I see the data received, I get "hi" received about once a second.

I have no idea how connector is supposed to be working, nor why a TaskPlugin is being scheduled.

Simon Wittber said...

Yes you are right, I need to write a better intro document, explaing why this might useful, and how it works. Thanks for the feedback.

Harish Mallipeddi said...

I don't know if it's just me but the Fibra 3 link on the first line doesn't work.

However I got the tarball from here: http://cheeseshop.python.org/pypi/fibra/

But when I run your example code against the version of Fibra from the tarball, it doesn't work.

Also on CheeseShop when I did a search for "fibra", I found this other project called FibraNet (http://cheeseshop.python.org/pypi/FibraNet/10) also started by you. I'm wondering what's this about?

Simon Wittber said...

FibraNet does similar things to fibra, however fibra uses some new Python 2.5 features to let values easily pass into and out of generators.

You can grab fibra 3 from here.

http://exactlysimilar.org/downloads/

Fibra 0-3 were just spike tests, 0.01 is the first version I might consider using in real code.

Popular Posts