mySun
mySun

Reputation: 1696

How to call a function without wait it finishes in php?

I use Laravel framework in my project.

I need call a function in PHP but I don't need wait for this.

For example:

public function payment($Authority)
{
   if (test == 1)
       $this -> one($Authority); // don't wait for this call.
   return view ("site.payment");

}

private function one($Authority)
{
   // php code
   // python code
}

Upvotes: 0

Views: 7767

Answers (3)

lagbox
lagbox

Reputation: 50491

Laravel has a queue job system. You could create a job to call that code and have your payment method dispatch the job to the queue for processing. (assuming you don't use the sync driver).

"Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time. Deferring these time consuming tasks drastically speeds up web requests to your application." - Laravel 5.3 Docs - Queues

public function payment($Authority)
{
    if (test == 1) {
        // send to queue for processing later
        dispatch(new SomeJob($Authority));
    }

    return view ("site.payment");
}

Upvotes: 1

kb0
kb0

Reputation: 1153

You can try to use PThreads extension (http://php.net/pthreads):

<?php
// create your own class from Thread
class MyWorkerThreads extends Thread
{
    private $workerId;
    private $authority;

    public function __construct($id, $authority)
    {
        $this->workerId = $id;
        $this->authority = $authority;
    }

    // main function
    public function run()
    {
        echo "Worker #{$this->workerId} ran" . PHP_EOL;
        echo $authority;

        // make some long run tasks
        $html = file_get_contents('http://google.com?q=testing');
    }
}

...
$worker = new WorkerThreads($i, $Authority);
// start new thread with long run task
$worker->start();
...
// You can wait for the job to be finished at any time, using join
$worker->join();

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28529

you can call another process to run the code with proc_open. You also can write it into a artisan command, then call the command run in another process.

Here is a example for use proc_open()

Upvotes: 0

Related Questions