Laravel中的多线程 [英] Multi-Threading in Laravel

查看:1209
本文介绍了Laravel中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我的数据库调用大大降低了页面加载速度.我正在从选举数据中填充多个图表,我的表包含大约一百万行,因此必须在getCharts()方法中的每个方法中多次查询此数据.

I am running into a problem where my database calls are slowing up the page load significantly. I am populating multiple charts from elections data, and my table contains around 1 million rows, and I have to query this data multiple times in each methods inside the getCharts() method.

使用此将返回数据传递给JavaScript.

I'am using this to pass the return data to JavaScript.

当您单击数据点时,将重新填充这些图表.因此,如果您点击某个点(即民主人士"),它将重新加载页面并再次调用这些方法.

These charts gets re-populated when you click on a data point. So if you click on a point i.e ('democrat) it will reload the page and call these methods again.

我要问的是,是否有可能在本机PHP中执行类似的操作.服务器在linode上运行PHP 5.2.

What I am asking is wether it is possible to do something like this in native PHP. The server is running PHP 5.2 on linode.

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');
    }

样本方法

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 = $query->get();
        $chartData = $this->prepare_data_for_chart($chartData, 'party', 'numVotes');
        JavaScript::put(['partyChart' => $chartData]);

    }

推荐答案

在PHP中可以进行多线程处理,之前在Stackoverflow上进行了讨论:

Multithreading is possible in PHP which has been discussed on Stackoverflow before: How can one use multi threading in PHP applications

但是我不知道多线程在这里能为您提供多少帮助.我们正在谈论多少数据,您在图表中寻找什么样的准确性?正在显示多少张图表?许多解决方案将来自上下文.

However I don't know how much multithreading will help you here. How much data are we talking, what kind of accuracy are you looking for in the charts? How many charts are being shown? A lot of the solution will come from context.

如果是我,我将有一个背景计划预处理巨大" "将大量数据转化为前端更有用/更有用的东西.再次有用/可用完全取决于上下文.当有实际的页面请求时,您只需要传递处理后的数据,而不是实际的实时"数据.

If it were me, I would have a background schedule pre-processing the "huge" amounts of data into something more useful/usable on the frontend. Again useful/usable entirely depend on the context. When there is an actual page request, you just need to pass down the processed data, versus actual "live" data.

同样,这将完全取决于您的应用程序的上下文.也许您需要即时数据,也许您不需要.

Again, again, this will entirely depend on the context of your application. Perhaps you need by-the-minute data, maybe you don't.

这篇关于Laravel中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆