Datastax/Cassandra module php don't work with pcntl_fork

Datastax/Cassandra perfect work in single script. But I need create many fork with pcntl_fork. And in fork cassandra don't work. simple script

    <?php
$cluster   = Cassandra::cluster()                 // connects to localhost by default
                 ->build();
$keyspace  = 'system';
$session   = $cluster->connect($keyspace);        // create session, optionally scoped to a keyspace
$statement = new Cassandra\SimpleStatement(       // also supports prepared and batch statements
    'SELECT keyspace_name, columnfamily_name FROM schema_columnfamilies'
);
$future    = $session->executeAsync($statement);  // fully asynchronous and easy parallel execution
$result    = $future->get();

If run as single script $result content data. If I create pcntl_fork script freezes on $future->get(). How fix?

Upvotes: 0

Views: 128

Answers (1)

Fero
Fero

Reputation: 406

The problem is all the children and the parent processes share the same underlying sockets and there isn't a portable or robust way for the php-driver to handle this. A good way to handle this in your application is to connect after the fork; here is some psuedo code for a single forked process:

$cluster = Cassandra::cluster()->build();

// Perform fork

if ($pid) { // Parent process
    $session = $cluster->connect($keyspace);

    // Do parent stuff
} else { // Child process
    $session = $cluster->connect($keyspace);

    // Do child stuff
}

Upvotes: 0

Related Questions