Skip to content

Reading a file throws error or hangs #5

@Zizaco

Description

@Zizaco

With the following code:

<?php
require "vendor/autoload.php";

$loop = \React\EventLoop\Factory::create();
$filesystem = \React\Filesystem\Filesystem::create($loop);
$loaded = [];

// I have "big_file_5.txt" and "big_file_7.txt".
// Both with about 6 MB.
foreach (['5', '7'] as $num) {
    $filename = __DIR__.'/resources/big_file_'.$num.'.txt';

    $file = $filesystem->file($filename);
    $file->getContents()->then(
        function ($content) use ($filename, &$loaded) {
            echo "Got contents of $filename\n";
            $loaded[] = $content;
        },
        function ($error) use ($filename, &$loaded) {
            echo "Error when reading $filename\n";
            echo $error;
        }
    );
}

$loop->addPeriodicTimer(1, function () use (&$loaded) {
    echo count($loaded)." loaded files\n";
    echo "tick-tack\n";
});

echo "Running. Press CTRL+C to exit\n";
$loop->run();

The output should be something along the lines of:

$ php main.php
Running. Press CTRL+C to exit
Got contents of <project_path>/resources/big_file_7.txt
Got contents of <project_path>/resources/big_file_5.txt
2 loaded files
tick-tack
2 loaded files
tick-tack
2 loaded files
tick-tack
2 loaded files
tick-tack
^C

But sometimes the Eio throws an unknown error

$ php main.php 
Running. Press CTRL+C to exit
Error when reading <project_path>/resources/big_file_7.txt
exception 'React\Filesystem\Eio\RuntimeException' with message 'Unknown error calling "eio_open"' in <project_path>/vendor/react/filesystem/src/EioAdapter.php:280
Stack trace:
#0 <project_path>/vendor/react/filesystem/src/EioAdapter.php(254): React\Filesystem\EioAdapter->executeDelayedCall('eio_open', Array, -1, Object(React\Promise\Deferred))
#1 [internal function]: React\Filesystem\EioAdapter->React\Filesystem\{closure}(Object(React\EventLoop\LibEventLoop))
#2 <project_path>/vendor/react/event-loop/Tick/FutureTickQueue.php(46): call_user_func(Object(Closure), Object(React\EventLoop\LibEventLoop))
#3 <project_path>/vendor/react/event-loop/LibEventLoop.php(202): React\EventLoop\Tick\FutureTickQueue->tick-tack()
#4 <project_path>/main.php(32): React\EventLoop\LibEventLoop->run()
#5 {main}Got contents of <project_path>/resources/big_file_5.txt
1 loaded files
tick-tack
1 loaded files
tick-tack
^C

Or one or both reads hangs forever

Running. Press CTRL+C to exit
Got contents of <project_path>/resources/big_file_7.txt
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
1 loaded files
tick-tack
... (forever)

Every time I run the script I got one of the results randomly(?). I'm running Ubuntu 14.04.1 LTS.

@WyriHaximus, do you have an idea of what may be causing the problem?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions