[Stackless] Asynchronous file object w/libevent (compared to IOCP)
Arnar Birgisson
arnarbi at gmail.com
Sat Dec 1 01:38:28 CET 2007
Hey there,
I was messing around with a libevent (via pyevent) stacklessfile, my
naive implementation is below. There is also a sleep function for
convenience.
Now these are just initial experiments, for example the event loop
tasklet never exits (one has to ctrl-c out of it). However, I looked
at the IOCP example at stacklessexamples. Seems it is doing *alot* of
stuff, am I missing some fundamental things in the libevent thingy
below?
cheers,
Arnar
import stackless
import event
def loop():
while True:
event.loop(True)
stackless.schedule()
def sleep(seconds):
def wakeup(ch):
ch.send(None)
ch = stackless.channel()
event.timeout(seconds, wakeup, ch)
ch.receive()
stdfile = file
class ssfile(object):
def __init__(self, *args, **kwargs):
self.f = stdfile(*args, **kwargs)
self.ch = stackless.channel()
self.closed = self.f.closed
self.encoding = self.f.encoding
self.mode = self.f.mode
self.name = self.f.name
self.newlines = self.f.newlines
self.softspace = self.f.softspace
self.close = self.f.close
self.flush = self.f.flush
self.fileno = self.f.fileno
self.isatty = self.f.isatty
self.readline = self.f.readline
self.seek = self.f.seek
self.tell = self.f.tell
self.truncate = self.f.truncate
self.writelines = self.f.writelines
def read(self, *args):
def cb():
self.ch.send(self.f.read(*args))
event.read(self.fileno(), cb)
return self.ch.receive()
def write(self, *args):
def cb():
self.ch.send(self.f.write(*args))
event.write(self.fileno(), cb)
return self.ch.receive()
if __name__ == "__main__":
def testsleep(sec):
print "going to sleep for %d secs" % sec
sleep(sec)
print "woke up after %d!" % sec
def writefile(i):
print "open file", i
f = ssfile('test%d.txt' % i, 'w')
print "write file", i
f.write('test123 + %d\n' % i)
print "close file", i
f.close()
stackless.tasklet(testsleep)(1)
stackless.tasklet(testsleep)(2)
stackless.tasklet(testsleep)(3)
stackless.tasklet(writefile)(1)
stackless.tasklet(writefile)(2)
stackless.tasklet(writefile)(3)
stackless.tasklet(loop)()
stackless.run()
More information about the Stackless
mailing list