[Web-SIG] Iterators, generators and threads.
Phillip J. Eby
pje at telecommunity.com
Mon Sep 6 15:47:03 CEST 2004
At 01:07 PM 9/3/04 +0100, Alan Kennedy wrote:
>Offhand, I can't think of scenarios where a WSGI server or application
>would *need* to iterate over an iterable across multiple threads. But I
>can certainly think of multiple server architectures where the request and
>its related response will pass through multiple threads before completion.
>Whether or not it would make sense for such architectures to iterate an
>iterable from multiple threads: well, I don't know: is it possible some
>server designer might attempt something like this?
>Which would probably work as long as the iterable is not a generator. But
>if it is: *boom*, the generator could be resumed simultaneously from
>multiple threads, thus resulting in a ValueError.
Generators don't actually add a new problem here. Pretend we're talking
about a list object instead. If you were to "resume it simultaneously from
multiple threads", what would happen? Well, you'd send items twice, or out
of order. So, obviously, you can't iterate over *any* iterable returned by
a WSGI app from multiple threads unless you serialize the access.
More information about the Web-SIG