php Laravel 中的多线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36830979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:57:34  来源:igfitidea点击:

Multi-Threading in Laravel

phpmultithreadinglaravelasynchronouslaravel-queue

提问by Yasin Yaqoobi

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.

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

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

我正在使用它来将返回数据传递给 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.

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

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.

我要问的是是否有可能在原生 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');
    }

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

    }

回答by Chris

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

多线程在 PHP 中是可能的,之前已经在 Stackoverflow 上讨论过:如何在 PHP 应用程序中使用多线程

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 schedulepre-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.

同样,这将完全取决于您的应用程序的上下文。也许您需要按分钟的数据,也许您不需要。

回答by Adam

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.

克里斯是对的 - 这完全取决于您的应用程序,但是如果您到达应用程序不可用的地步,您应该考虑在 redis 或 memcached 中兑现您的结果。