2015. 3. 8.

using event loop for aync funtion

use v5.12;
use AnyEvent;
use Search::Elasticsearch::Async;

# EV must be installed
use Promises (backend => ['EV'], 'deferred');

my $cv = AnyEvent->condvar;

my $e = Search::Elasticsearch::Async->new(

nodes=>'http://localhost:9200'
);


my $doc_size =1000;
my $success_count = 0;

my $bulk = $e->bulk_helper(
index=>'my_app',
type=>'blog_post',
on_success => sub {
#say "success";

},
on_error => sub{
say "fail";
}
);


my @arr_res =();


sub main_loop{


my $idx =0;
@arr_res = ();
for (1..$doc_size)
{
push (@arr_res, '{"a": 1111, "b":222, "name":"KKKKKK"}');

}
$bulk->create_docs(@arr_res)->then (
sub {
say "bulk ok";


}, ##success
sub {
say "bulk fail";
} #fail.
);
$bulk->flush();

}

sub start_ {
my $d = deferred;

    my $w;
    $w = AnyEvent->io(
        fh   => \*STDIN,
        poll => 'r',
        cb   => sub {
            chomp( my $input = );
            undef $w;
            # resolve the promise
            $d->resolve($input);
        }
    );

    # return a promise
    return $d->promise;
}


sub main {

start_()
->then( \&main_loop)
->then(sub { print "fin.\n"; })
->catch( sub { print "errors";})
->finally( \&main );

}


main();
EV::run;


2015. 3. 7.

Change vi color scheme

It is based on http://kb.mediatemple.net/questions/1565/Enabling+vi+syntax+colors#gs

1) open .vimrc    at home directory
2) Add below codes.
      syntax on
      colorscheme desert
3) Save it.
4) open code file by vi.

** color scheme list
    default, desert, elflord...

Enjoy it.


2015. 3. 6.

perl HTTP:Async call sample

use HTTP::Async;
use LWP::UserAgent;

my $async = HTTP::Async->new;

for($idx=0; $idx<10; $idx++){
 
 $async->add( HTTP::Request->new( GET => 'http://www.daum.com/'         ) );
    $async->add( HTTP::Request->new( GET => 'http://www.google.com/' ) );
}



while ( my $response = $async->wait_for_next_response ) {
        # Do some processing with $response
        my $comple = $async->to_return_count;
        my $total = $async->total_count;
        my $progress = $async->in_progress_count;
       print "complete:  $comple, total:$total, progress:$progress \n";
    }