简体   繁体   English

web 机器学习,PHP 连接 python

[英]Machine Learning on web, PHP connection with python

I'm currently working on a ML project with geo data...我目前正在使用地理数据进行 ML 项目......

In my web I let the models parameters of the machine learning algorithm to the user, then I send those to an Apache server where PHP gets the parameters... In js I use Ajax to make the POST request.在我的 web 中,我将机器学习算法的模型参数发送给用户,然后将这些发送到 Apache 服务器,PHP 获取参数...在 js 中,我使用 Ajax 发出 POST 请求。

My ML algorithm is made with Python, right now is working with the library argparse to read the parameters that PHP sends (after of a verification) as command trough the function exec()我的 ML 算法是用 Python 制作的,现在正在使用库argparse读取 PHP 作为命令通过 function exec()

I have 2 problems with this:我有两个问题:

  1. If the ML model takes time to calculate the results, the exec function does't wait for them and returns null after some time, but if it's fast everything is ok.如果 ML model 需要时间来计算结果,则exec function 不会等待它们并在一段时间后返回null ,但如果速度很快,则一切正常。 I already have the function set_limit_time(0);我已经有了 function set_limit_time(0); in my PHP file.在我的 PHP 文件中。

  2. In my local machine it doesn't take so much time to calculate results like on the server, but the server has better hardware, so I don't know what is going on there.在我的本地机器上,计算结果不像在服务器上那样花费太多时间,但是服务器有更好的硬件,所以我不知道那里发生了什么。


  • PHP 7.0.15 PHP 7.0.15
  • Python 2.7 Python 2.7
  • Server Apache/2.4.18 (Ubuntu 16.04.1 LTS)服务器 Apache/2.4.18 (Ubuntu 16.04.1 LTS)

Also, is there a better way to do this?.另外,有没有更好的方法来做到这一点?

I'd like to suggest another approach.我想建议另一种方法。

Whenever you call your algorithm from the command line, you have a bootstrapping time: importing libraries (such as numpy), loading datasets, etc. Then you perform your calculations, return a response and clear the memory. So the next time you need another result, you have to go through all the process again.每当你从命令行调用你的算法时,你都有一个引导时间:导入库(如 numpy),加载数据集等。然后你执行你的计算,返回一个响应并清除 memory。所以下次你需要另一个结果,你必须 go 再走一遍所有的过程。

I'd suggest embedding that algorithm inside a small Flask application (you already know Python and know how to use PHP for web, it shouldn't be that hard.我建议将该算法嵌入到一个小的 Flask 应用程序中(您已经知道 Python 并且知道如何将 PHP 用于 web,应该不会那么难。

Since your python web server will have all the libraries and the datasets already loaded, it would be much faster in answering your questions.由于您的 python web 服务器将加载所有库和数据集,因此回答您的问题会快得多。

And you can access it from PHP doing an HTTP request with curl (it has timeout.).您可以从 PHP 访问它,使用 curl 执行curl请求(超时)。

I think this would be much easier and scalable.我认为这会更容易和可扩展。

Just my two cents!只是我的两分钱!

I ended up opening a Python Socket and connecting PHP trough it, now the process it's still slower than my local machine but the timeout is not longer a problem.我最终打开了一个 Python 套接字并通过它连接了 PHP,现在这个过程仍然比我的本地机器慢,但超时不再是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM