相关疑难解决方法(0)

Laravel 中的多线程

我遇到了一个问题,我的数据库调用显着减慢了页面加载速度。我正在从选举数据中填充多个图表,我的表包含大约 100 万行,我必须在方法内的每个方法中多次查询这些数据getCharts()

我正在使用它来将返回数据传递给 JavaScript。

当您单击数据点时,这些图表会重新填充。因此,如果您单击一个点 ie ('democrat),它将重新加载页面并再次调用这些方法。

我要问的是是否有可能在原生 PHP 中做这样的事情。服务器在 linode 上运行 PHP 5.2。

foreach(function in getChartsMethod){
     Start a child thread to process the function. 
}  
join the threads. 
reload the page. 


public function getCharts(){
        $this->get_cast_chart();
        $this->get_party_chart();
        $this->get_gender_chart();
        $this->get_age_chart();
        $this->get_race_chart();
        $this->get_ballot_chart();
        $this->get_county_chart();
        $this->get_precinct_chart();
        $this->get_congressional_district_chart();
        $this->get_senate_district_chart();
        $this->get_house_district_chart();
        return view('elections.index');
    }
Run Code Online (Sandbox Code Playgroud)

样品方法

public function get_party_chart(){
        $query = DB::table($this->tableName)
            ->select('party', DB::raw('count(*) as numVotes'))
            ->groupBy('party')
            ->orderBy('numVotes', 'ASC');

        if ($this->filterParameters != null){
            $query = $query->where(key($this->filterParameters), $this->operator, current($this->filterParameters));
        }
        $chartData = …
Run Code Online (Sandbox Code Playgroud)

php multithreading asynchronous laravel laravel-queue

4
推荐指数
1
解决办法
3万
查看次数