How do you call a function without waiting for it to end?

Asked

Viewed 183 times

2

I need to call a function in PHP that records several records in the database which will take a long time, I need it to do in "background" while PHP does something else, is it possible? If yes, how to do?

  • 2
  • 3

    We have 2 or 3 questions on the site that deal with running time-consuming PHP operations via command line + crontab. It is probably a better solution for what you describe. Nothing prevents the online application from something in a queue for the BG task to perform. Usually better than thread parallelism.

1 answer

3


You can use the class Thread of PHP:

<?php

class segundoPlano extends Thread 
{
    public function __construct($sP)
    {
        $this->sP = $sP;
    }

    public function run()
    {
        //o teu código a executar;
    }
}

$segundoP = new segundoPlano($sP);
$segundoP->start();

The variable in the constructor is an example.

Note: To use this class you must install the pthreads extension.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.