Created
March 19, 2011 05:46
-
-
Save awwaiid/877257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Boiled down to it's essentials? This is how to write a twiggy-compatible | |
# Coro-using PSGI app. | |
use strict; | |
use EV; | |
use Coro::AnyEvent; | |
use Coro; | |
my $app = sub { | |
my $env = shift; | |
unless ($env->{'psgi.streaming'}) { | |
die "This application needs psgi.streaming support"; | |
} | |
return sub { | |
my $respond = shift; | |
async { | |
# This is where you do some long-running but non-blocking things. | |
# Note that you can write out as you go if you like. | |
# You could use Coro::AnyEvent::idle to just yield to other requests. | |
my $writer = $respond->([200, ['Content-Type', 'text/plain']]); | |
foreach my $i (1..10) { | |
print "Writing $i...\n"; | |
$writer->write( time() . " count $i\n"); | |
Coro::AnyEvent::sleep 1; | |
} | |
# When you're done doing stuff, feel free to close the writer. | |
# Technically scope will close it too. | |
$writer->close; | |
}; | |
}; | |
}; | |
return $app; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment