如何在不等待 php 完成的情况下调用函数?
How to call a function without wait it finishes in php?
我在我的项目中使用 Laravel 框架。
我需要在 PHP 中调用一个函数,但我不需要等待它。
例如:
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
}
您可以调用另一个进程来 运行 使用 proc_open 的代码。你也可以把它写成一个artisan命令,然后在另一个进程中调用命令运行。
这里有一个使用示例
您可以尝试使用 PThreads 扩展 (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();
Laravel 有一个队列作业系统。您可以创建一个作业来调用该代码,并让 payment
方法将作业分派到队列中进行处理。 (假设您不使用 sync
驱动程序)。
"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");
}
我在我的项目中使用 Laravel 框架。
我需要在 PHP 中调用一个函数,但我不需要等待它。
例如:
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
}
您可以调用另一个进程来 运行 使用 proc_open 的代码。你也可以把它写成一个artisan命令,然后在另一个进程中调用命令运行。
这里有一个使用示例
您可以尝试使用 PThreads 扩展 (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();
Laravel 有一个队列作业系统。您可以创建一个作业来调用该代码,并让 payment
方法将作业分派到队列中进行处理。 (假设您不使用 sync
驱动程序)。
"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");
}