Skip to content

Instantly share code, notes, and snippets.

@awwaiid
Created March 19, 2011 05:46
Show Gist options
  • Save awwaiid/877257 to your computer and use it in GitHub Desktop.
Save awwaiid/877257 to your computer and use it in GitHub Desktop.
# 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