njlg / perl-rethinkdb

A Pure Perl RethinkDB Driver

Home Page:http://njlg.info/perl-rethinkdb

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changefeeds dont block

matt01 opened this issue · comments

The following code (taken from lib/Rethinkdb/Query/Table.pm) :

my $changes = r->db('test')->table('table')->changes->run();  
foreach(@{$changes}) {
 print Dumper($_);
}

Produces the error:

Not an ARRAY reference at

Changing foreach(@{$changes}) to foreach(@{$changes->{response}}) runs but dosn't block waiting for changes.

And the following:

while (my $changes = r->db('test')->table('table')->changes->run()) { foreach(@{$changes->{response}}) { print Dumper($_); } }

returns the changefeed but causes excessive load

Thanks for the report (and checking out the project). I'll take a look and see what is going on.
Feel free to submit a merge request if you have a good idea on how to solve the issue.

Okay. I looked in to this issue and there is no great solution for what you want to do. I do not necessarily want to block the execution context on a changes() since it returns a feed and means that you have to subscribe to it and then decide when you want to stop listen for changes.

However, I am going to change run() to take a callback function to allow a better subscription API. Here is an example of how you could use it:

use feature ':5.16';
use Data::Dumper;
use Rethinkdb;

# setup
r->connect->repl;
r->db('test')->create->run;
r->db('test')->table_create('table')->run;

# receive changes
r->db('test')->table('table')->changes->run(sub {
  my $changes = shift;
  if( @{$changes->response} ) {
    say {*STDOUT} Dumper($changes);
  }
});

# force the script to stay open
while(1) {}

And then run this:

use Rethinkdb;

# connect
r->connect->repl;

# insert a bunch of things
foreach(1..100) {
  r->db('test')->table('table')->insert({count => $_})->run;
}

Thoughts?

When registering a changes callback this will block forever waiting for changes to come in with version 0.11.

#!/usr/bin/env perl
use feature ':5.22';
use Data::Dumper;
use Rethinkdb;

# setup
r->connect->repl;
r->db('test')->create->run;
r->db('test')->table_create('table')->run;

# receive changes
r->db('test')->table('table')->changes->run(sub {
  my $changes = shift;
  if( @{$changes->response} ) {
    say {*STDOUT} Dumper($changes);
  }
});

I am going to close this now.