Multi-Threading in Laravel

天大地大妈咪最大 提交于 2019-12-07 09:21:16

问题


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.

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.

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

Sample method

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]);

    }

回答1:


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.




回答2:


Chris is right - it all depends on your application, but if you are are getting to the point where application is not usable, you should look in into cashing your results in redis or memcached.



来源:https://stackoverflow.com/questions/36830979/multi-threading-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!