简体   繁体   中英

How to use threading in Laravel to run stored procedures

I want to use php threads for asynchronously loading a function that executes a mysql stored procedure. The stored procedure takes a lot of time to load, so keeping it asynchronous is the only optimal solution, I found.

I have no idea on how to bring the threading inside Laravel. Laravel has queues but I want to do it directly in script with thread.

What i've done to approach a similar issue (I've done it in a sync command) is to create a class that extends from Thread and call it from the laravel code.

The class in your case might be something like this:

class LaravelWorker extends Thread
{
    private $object;

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

    public function run()
    {
        $object->runProcedure();
    }
}

And you can call it at your code such as this:

$object = new ObjectWithProcedure();
$threadedMethod = new LaravelWorker($object);
$threadedMethod->start();

If, for some reason, you need to wait until the $threadedMethod finishes, you can do

$threadedMethod->join();
(more_code...)

And the more_code section will only execute once $threadedMethod has ended.

Hope it helps!

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