简体   繁体   中英

Background php function processing

I am using Ci- Doctrine here...

I have 3 tables named exchange_rate , exchangerate_batch and agents .

exchange_rate and exchangerate_batch have many to one relationship and the 'agents' have many to one relationship with exchangerate_batch .

While updating exchange_rate table, the updated rates of different countries are added as new rates together with the old rate and a new exchangerate_batch is created and linked with the exchange rate.

The agent table consists of mainagents and subagents on the same table where they are related as one to many but on the same table.

I do not have any problem inserting and generating new batch ids. The real problem starts when i have to update the new created id of exchangerate_batch table on each agents and subagents on my agent table. The exchange rate is different for different agent. I need to search all the sub agents of the particular agents and update the exchangerate_batch id.

One solution would be to run the insert of the exchange rate and batch and redirect to the same page while running the update of agents on the background so that the user dont have to wait for the process to continue.

How can we do that. I have hear shell_exec can do it but not sure how to use it. Any idea on PHP how to use background process so that i could update the agent table in the background.

Sorry for the trouble,,

Write your Updatecommand in a Seperate php File (or any other Application)

With Shell execute in the PHP Case:

shell_exec('<pathto>php.exe -f <pathto>script.php');

On the otherhand there is a good extension "PThreads". You can start a Background process with it, and can synchronize after that.

http://www.php.net/manual/en/book.pthreads.php

class BackgroundSql extends Thread {
    private $_sql;
    public $result;

    public function __construct($sql){
        $this->_sql = $sql;
    }

    public function run() {
        //Execute your Query with your Db Class and Save Result
        $this->result = $Db::Query($this->_sql);
    }
}

$updateCommand = new BackgroundSql('Update ... SET ... = ...');

if ($updateCommand->start()) {

    /* do some work */
    sleep(10);

    /* ensure we have data */
    $updateCommand->join();

    var_dump($updateCommand->result);
}

Edit: Made more specific

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM