We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon not able to load multiple pages at once

Hi, I'm using Phalcon 2.0 in our internal web page. We have this problem: While Phalcon is loading a large piece of code, for example this one:

    set_include_path(APP_PATH . "/app/vendor/phpseclib");
    include_once APP_PATH . "/app/vendor/phpseclib/Net/SFTP.php";

    if (!defined('NET_SFTP_LOGGING')) define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
    $sftp = new Net_SFTP($server);

    if (!$sftp->login($username, $password)) {
        echo 'Login Failed.';
        echo $sftp->getSFTPLog();
    } else {
        echo 'Connected by SFTP.<br>';
        if (!empty($files)) {
            if ($sftp->put($remote_directory . $name, $local_directory . $files, NET_SFTP_LOCAL_FILE)) {
                echo "Upload completed<br>";
            } else {
                echo "Fail uploading<br>";
            }
        }
    }

It uploads multiple and large files, and while is uploading it, no one page is responding until it finish. How can I avoid it generally? The above code is only an example.

Thanks



85.5k
edited Aug '16

its a bit too long to explain. Simple as that php runs line by line, same as function sleep. So its just waiting for the file to be uplaoded until it moves to the next line.

https://www.google.com/search?q=php%20workers&rct=j

https://devcenter.heroku.com/articles/php-workers

https://php.net/manual/en/gearmanclient.dobackground.php

people mostly uses them to send emials, cuz its the same crap when you want to send user activating link for example.

edited Aug '16

I searched a lot on internet about it but i didn't found so many examples running it with an MVC.

its a bit too long to explain. Simple as that php runs line by line, same as function sleep. So its just waiting for the file to be uplaoded until it moves to the next line.

https://www.google.com/search?q=php%20workers&rct=j

https://devcenter.heroku.com/articles/php-workers

https://php.net/manual/en/gearmanclient.dobackground.php

people mostly uses them to send emials, cuz its the same crap when you want to send user activating link for example.

edited Aug '16

You can put those file transfers into message queue. Phalcon supports beanstalkd out of the box, so you can give it a try (you need to install first beanstalkd daemon). That way, just after the browser sends all the buffered data to web server -> phalcon app, your app can continue it's execution flow.

But if the files are that large, your user will wait for physical upload anyway from their browser to the server. Since you're running an Intranet application, that should be 1 Gbps connection in LAN, so that's not an issue but SFTP going through entire Internet later on.

HTTP is a stateless procotol while PHP is Synchronous environment. There are ways to overcome those limitations.

But for the start just take a look at this example: https://gist.github.com/joepie91/dc67316d2a22f321d1a1

I refer here to node.js only to demonstrate the basic difference betweeen Synchronous and Asynchronous process.

edited Aug '16

So I understand that two simple PHP files can be executed at same time, but Phalcon execute config, loader and services.php and they are blocked until one page finishes, so the other page cannot load this config, loader and services.php files? Am I right?

I tested doing an sleep(100); and then execute another .php and it is loaded. I put an sleep(100); into any controller and all phalcon is blocked.



11.6k

as Stamster suggest, use Beanstalk and parallel CLI app for example, it's the way I choose in my app, processing longer task (disk write ops) from a cli script filled with data from web app thru beanstalk queue. As php-cli and regular php are two separate proccess, they can run in concurency on the same server. and if you need to send some feedback from cli to your web app when background job is finished, you can use websocket..

edited Aug '16

So I understand that two simple PHP files can be executed at same time, but Phalcon execute config, loader and services.php and they are blocked until one page finishes, so the other page cannot load this config, loader and services.php files? Am I right?

PHP files are all separate process (child processes from main PHP binary).

Phalcon loads different files as you told it (him?) to do. Normally those are components - loader, config, services etc. If you try to open new tab from the same user session, you should be able to load something else which is non blocking (let's say homepage).

I tested doing an sleep(100); and then execute another .php and it is loaded. I put an sleep(100); into any controller and all phalcon is blocked.

Well, if you put something blocking in services container or anywhere where the app might need it during bootstrap (i.e. on every page), you're stuck. Such blocking and long running tasks should be only called per controller. The file after sleep is loaded only after sleep finishes execution. There's no way to see the page untill sleep finishes w/o using something from the pcntl_ universe.