Stackless python and microthreads

P

Peter Hansen

Valentino said:
Hi Michele :).

If all you need is s-expr you maybe can look at Nevow (pronounced
'nuevo'), which is the new web toolkit built on top of twisted.web.

Which raises the question, how do you pronounce "nuevo"?

(Perhaps noo-VOH, as in the French nouveau?)

-Peter
 
E

Erik Max Francis

Peter said:
Which raises the question, how do you pronounce "nuevo"?

(Perhaps noo-VOH, as in the French nouveau?)

Noo-WAY-voh. (More like NWAY-voh, actually.)
 
M

Michele Simionato

Michael Walter said:
Ville said:
"Michele" == Michele Simionato <[email protected]> writes:
Michele> of learning Zope TAL/TALES/METAL, which I do not like
Michele> that much; I would rather prefer to generate HTML pages
Michele> with Lisp macros and use real s-expressions instead of
Michele> XML/HTML, but I can't :-(
What would you do with macros? [..]
Syntactic abstraction. I would love to see macros in Python (actually
working on some ideas for adding them to Python).

NO!! I am really talking about prefix notation: I am pro macros when
you have prefix notations and s-expressions (i.e. HTML/XML), whereas
I am not advocating macros in Python, which has an infix notation.

There are macro systems based on pattern matching which can work with
infix notation (I hear Dylan has one), but I feel somewhat happier with
Lisp-style macros where everything is a list.


Michele Simionato
 
M

Michele Simionato

Valentino Volonghi aka Dialtone said:
Hi Michele :).

If all you need is s-expr you maybe can look at Nevow (pronounced
'nuevo'), which is the new web toolkit built on top of twisted.web.

It has a template syntax that is similar to ZPT.

And it has stan which is an s-expr-like syntax.

Here is a little helloworld example:

docFactory = rend.stan(
T.html[
T.head[
T.title['Hello'],
],
T.body[
T.p['Welcome to the wonderful world of Nevow!'],
],
]
)

Interesting. But I have to check which facilities it provides to manipulate
s-expressions; if I was in a pervers mood, I could even generate nevow s-
expressions from Scheme ;)

Michele
 
D

Donn Cave

Quoth Peter Hansen <[email protected]>:
| Valentino wrote:
....
| > If all you need is s-expr you maybe can look at Nevow (pronounced
| > 'nuevo'), which is the new web toolkit built on top of twisted.web.
|
| Which raises the question, how do you pronounce "nuevo"?

Good question. I thought I knew how to pronounce "nuevo", but if it
could be spelled "nevow" ... is that twisted, or what?

Donn
 
M

Michael Walter

Michele said:
Michael Walter said:
Ville said:
Michele> of learning Zope TAL/TALES/METAL, which I do not like
Michele> that much; I would rather prefer to generate HTML pages
Michele> with Lisp macros and use real s-expressions instead of
Michele> XML/HTML, but I can't :-(
What would you do with macros? [..]

Syntactic abstraction. I would love to see macros in Python (actually
working on some ideas for adding them to Python).

NO!! I am really talking about prefix notation: I am pro macros when
you have prefix notations and s-expressions (i.e. HTML/XML), whereas
I am not advocating macros in Python, which has an infix notation.
And my point was that I'm advocating macros in non-s-expression
languages, too, as I think they are equally valuable there.
There are macro systems based on pattern matching which can work with
infix notation (I hear Dylan has one), but I feel somewhat happier with
Lisp-style macros where everything is a list.
I'm pretty sure (and my experiments seem to indicate that, too) that you
can have both macros defined by pattern-matching and "raw" macros which
operate on the AST in Python, too.

Yes, Dylan has macros, too, despite its syntax.

Cheers,
Michael
 
C

Christian Tismer

Donn said:
Quoth Peter Hansen <[email protected]>:
| Mike C. Fletcher wrote:
|
| > continuations are the most awesome toys
| > you can imagine... we will all mourn their passing in time...
|
| Will that be _before_, or _after_ we understand what they are? ;-)

I gave them a try, and I mourn them with a touch of relief, I guess.
I'm the guy who won't bother to learn list comprehensions and generally
despises cool gimmicks, but continuations were more than just cool,
they were terribly powerful.

Yes. And terribly hard to maintain as well :)
I hooked them into a graphics toolkit wrapper with an event dispatching
model where each window, network connection etc., ran in its own native
thread. No blocking, always ready to respond to a new message. When a
couple of these threads have to interact, they would queue messages back
and forth, and the programming paradigm can get awkward - a lot of state
can pile up as everyone's trying to keep track of their progress through
some kind of procedure.

Well, suppose you send your message and save the whole function state
somewhere, and return to the dispatcher. When the response comes back,
you pick up the function state and continue it, feeding the response in.

Ok, that's a nice pattern which I use as well, *with* current Stackless
and *without* continuations. Turning control flow inside out does not
depend upon continuations. You just need the ability to switch contexts,
which is in fact much less, but buys you a lot more.

A continuation is immutable. That means it can be activated multiple
times and it will not change state. How to explain this? Well, consider
a Python function, and you have several continuations captured from
that running function. Then you can jump at any continuation at any
time, as often as you like. This capability is incredible, but I never
saw an example of code that *really* had to have this feature.

Returning to some point in the running program and continuing there
is what most people need. This is less than a general continuation,
because the continuation is gone after resuming. You can call it
a one-shot continuation. On every context switch, the current
continuation is preserved, but it is never fired twice. This is very
much like threads.

Full continuations enforce that you have complete control over
the current program, especially you need to be able to track
all object references which must stay alive in a continuation.
This is not possible if your program state contains hidden
objects on the C stack, because we have no introspection feature
in the Python C implementation.
In other words: You cannot use continuations in the context of
C extensions.

Restricting ourselves to one-shot continuations, we don't need
to track any objects explicitly. This makes it possible to
deal with hidden objects just by saving and restoring parts of
the C stack.

And this makes tasklets more powerful that the old continuations:
it is possible to switch context all the time. The program
environment does not need to collaborate. Collaboration is more
efficient (i.e. trying to leave no state on the C stack), and
Stackless tries to do it almost everywhere. But that's not
a limitation. You can write ordinary C code and ask Stackless to
switch tasklets, and it will switch. I consider this more
valuable than continuations which pretend to be universal and
immportal, but only in a restricted context.
Now instead of a tedious kind of state machine, you're writing an plain,
ordinary function that sends messages to its peer and looks at the response,
as though that were all synchronous, and it's so much simpler. Yet the
execution underneath that isn't synchronous at all, because your computation
is suspended in between your send and the response. It really does return
every time it sends a message, it just starts up next time where it left
off.

Exactly what I'm doing with Stackless. I have my own implementation
of sockets and files, which pretend to have synchronized calls
only, but under the hood they are asynchronous, dispatching over
a poll/select loop and optimizing throughput. The code is much
shorter than asyncore, and reasonably more effective if your server
is computation-bound. Unfortunately I can't publish this module,
yet, it was contract work.

cheers - chris

--
Christian Tismer :^) <mailto:[email protected]>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
 
M

Matt Leslie

Infact this is also similar to what I'm trying to do, except I'm doing
it back to front. I want to write a package to help simulate distributed
systems, making what would be simple synchronous method calls
asynchronous. I do this by intercepting method calls between threads,
and passing them on only after a simulated network delay - they are held
in an event queue and basically only executed when there is nothing
scheduled to happen 'before' it. It boils down to a similar structure, I
think.

Anyway, stackless would be good for this because I need lots of threads
so simulate large scale P2P systems. The thing is although there was a
fair amount of documentation on microthreads I can find nothing usefull
on tasklets! Can anyone point me at some? Please? The stackless mailing
list has turned up nothing so far...

Matt
 
C

Christian Tismer

Matt Leslie wrote:

....
Anyway, stackless would be good for this because I need lots of threads
so simulate large scale P2P systems.

This is true. For performance comparison reasons, and to make
Stackless "complete" in a way, I just developed real thread support
and measured context switch speed. Now it is not longer a claim,
but the truth (going to be Stackless 3.1):

Stackless soft-switched channels are 10 times faster than
using real threads.

Stackless tasklets are 10000 times smaller than threads on Windows.
The thing is although there was a
fair amount of documentation on microthreads I can find nothing usefull
on tasklets! Can anyone point me at some? Please? The stackless mailing
list has turned up nothing so far...

There isn't much, of course. There is the beginning of a glossary
on the website, there are lots of exmaples in preparation
after the last sprint, but actually there is still a lack
of documentation.

The C API is there, just look into stackless_api.h.

Then, for a description of individual functions,

import stackless
help(stackless)

gives you a lot of information.

Feel free to ask me for support, I will help as much as I can.

cheers - chris

--
Christian Tismer :^) <mailto:[email protected]>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top