Peeps into an iterator by doing an iteration and returns an equivalent iterator.
(iterator)
| 289 | """Returns a WSGI-compatible function for this application.""" |
| 290 | |
| 291 | def peep(iterator): |
| 292 | """Peeps into an iterator by doing an iteration |
| 293 | and returns an equivalent iterator. |
| 294 | """ |
| 295 | # wsgi requires the headers first |
| 296 | # so we need to do an iteration |
| 297 | # and save the result for later |
| 298 | try: |
| 299 | firstchunk = next(iterator) |
| 300 | except StopIteration: |
| 301 | firstchunk = "" |
| 302 | |
| 303 | return itertools.chain([firstchunk], iterator) |
| 304 | |
| 305 | def wsgi(env, start_resp): |
| 306 | # clear threadlocal to avoid interference of previous requests |