Proposal for adding Shallow Threads and a Main Loop to Python

Diez B. Roggisch deetsNOSPAM at web.de
Fri Mar 18 05:16:13 EST 2005


Hi, 

a few questions:

> A shallow thread is just a generator modified in the most obvious way
> possible.  The yield statement is replaced with a waitfor expression.
> You give it the object you wish to "wait for".  Then when it's ready
> you get back a return value or an exception.  These waitfor expressions
> are the only points where your shallow thread may get suspended, so
> it's always explicit.  If you call another function it will be treated
> exactly as a normal function call from a generator (this is why they're
> 'shallow'), so there's no issues with calling C functions.
> 
> On the other end of things, your shallow thread object would have
> __resume__ and __resumeexc__ methods (or perhaps a single __resume__
> method with a default raise_=False argument).  They return a tuple of
> (mode,value) where mode is a string of 'waitfor', 'exception', or
> 'return' and value corresponds to that.  These methods will be used by
> a main loop to resume the shallow thread, like next() is used with a
> generator.

So basically a shallow thread could be written in today's python like this?

class ShallowThread(object):
    def __init__(self, data=None):
        self.data = data

    def run(self, initial_data=None):
        while True:
            ....
            yield <some_state>
            new_data = self.data
            ....

st = ShallowThread().run(<some_initial_data>)
while True:
    result = st.next()
    st.data = <some_subsequent_data>

Basically <some_state> is your above mentioned tuple, and waitfor is like
yield with a return value that I modeled with explicit state called data.
Is that correct so far? 

> import mainloop, urllib
> 
> def get_and_save(path):
>     infile = waitfor urllib.urlopen(path, async=True)
>     outfile = waitfor open(path.split('/')[-1], async=True)
>     waitfor outfile.write(waitfor infile.read(async=True), async=True)
>     infile.close()
>     outfile.close()
> 
> def main():
>     a = get_and_save("http://python.org/pics/PyBanner021.gif")
>     b = get_and_save("http://python.org/pics/pythonHi.gif")
>     c = get_and_save("http://python.org/pics/PythonPoweredSmall.gif")
> 
>     waitfor allDone(a, b, c)
> 
> if __name__ == "__main__":
>     mainloop.runUntil(main())
> 
> Well there you have it.  I've glossed over many details but they can be
> cleared up later.  What I need to know now is how everybody else thinks
> about it.  Is this something you would use?  Does it seem like the
> right way to do it?  And of course the all important one, can I get it
> in to python core?  <0.5 wink>

I've difficulties grasping where the actual work is done - the event
notifier thingies are sort of generators themselves, and the mainloop gets
them and calls some execute method on them?


And now the final $1000,0000.00 question - why all this? No offense intended
- it's a little bit more comfortable than the generators approach sketched
by others (e.g. David Mertz if I recall corretly) - but to my view, it
_could_ be done in today python because we have generators. Or not?
-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list