Twisted TV - fairly simple processes
Tuesday: Went to the office and worked hard, but unfortunately not on my game. Kept thinking that there had to be a simpler way of implementing coroutines.
Returned home in the evening and improved my code by taking things out, resulting in fsp.py - Fairly Simple Processes.
It means that I can write multitasking code like this, hopefully simplifying the logic a bit. It's all part of a wild and wacky experiment based on reading a few articles about Erlang.
from fsp import spawn, run
def producer(consumers):
while True:
yield True
for consumer in consumers:
consumer.send(me)
def consumer():
while True:
yield wait()
msg = recv()
print me, msg
c = spawn(consumer)
for i in xrange(10):
spawn(producer, (c,))
run()
Next: Re-integrate sprite stuff.
(log in to comment)
Comments
:)
Marlow on
2007/09/06 06:32:
Minor modifications to fsp.py (not uploaded yet)...Given that my code was dotted with things like...
renderer.send(('move', me, x, y))I overloaded __lshift__ to make it possible to writerenderer << ('move', me, x, y)That in itself cleaned up the appearance of my code, because sending asynchronous messages is fundamental to the approach I'm taking.
I then turned my attention to reading messages, and realised if I made the process class into an iterator then it would be possible to replace this...
while True: yield wait() while ready: msg = recv() *** do something with msg ***with this...while True: yield wait() for msg in me: *** do something with msg ***...where me is the name of the current process.I also threw in a broadcast() call, as a common idiom seems to be to send the same message to a bunch of processes.
My wife asked me this morning how my game was going. "Well, I now have a nice concurrent message passing library." Sooner or later a game will emerge.