0

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
}
LF00
  • 27,015
  • 29
  • 156
  • 295
mySun
  • 1,550
  • 5
  • 32
  • 52
  • PHP doesn't work this way. – axiac Mar 18 '17 at 06:55
  • Hi, @axiac , Really? solution do you have? – mySun Mar 18 '17 at 06:56
  • try sleep() function – A.A Noman Mar 18 '17 at 07:00
  • Hi @A.ANoman, How to use sleep() function in my code? – mySun Mar 18 '17 at 07:02
  • [`sleep()`](http://php.net/manual/en/function.sleep.php) doesn't help here. It only pauses the script execution for some amount of time (in seconds). – axiac Mar 18 '17 at 07:08
  • There are a couple of ways (using [`pcntl_fork()`](http://php.net/manual/en/function.pcntl-fork.php) or [program execution functions](http://php.net/manual/en/ref.exec.php)) that create a new process. The new process runs independent and it can continue running after the current script completes. – axiac Mar 18 '17 at 07:16

3 Answers3

1

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();
kb0
  • 1,143
  • 1
  • 8
  • 13
1

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");
}
lagbox
  • 48,571
  • 8
  • 72
  • 83
0

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()

Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295
  • This doesn't answer the question. The linked answer also doesn't provide any hint about how to answer to this question. – axiac Mar 18 '17 at 07:36